有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Studio下一页按钮不工作

我为我的应用程序创建了一个安卓 studio按钮,当我点击“继续”按钮时,它不起作用。我没有收到任何错误,只是不起作用。当用户单击“继续”按钮时,我想进入第一个测验。爪哇

我看过所有其他类似的问题,但似乎找不到我做错了什么

主要活动。爪哇

package com.example.john.quiz1;

import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {

            startActivityFromFragment(new Intent(MainActivity.this, FirstQuiz.class));

        }

        private void startActivityFromFragment(Intent intent) {
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
//noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

主要活动。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <TextView
        安卓:id="@+id/textView2"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentTop="true"
        安卓:layout_centerHorizontal="true"
        安卓:layout_marginTop="160dp"
        安卓:text="Welcome to Road Sign Quiz"
        安卓:textAppearance="?安卓:attr/textAppearanceLarge"
        安卓:textColor="#0000ff"
        安卓:textSize="24sp" />

    <Button
        安卓:id="@+id/button"
        style="?安卓:attr/buttonStyleSmall"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_below="@+id/textView2"
        安卓:layout_centerHorizontal="true"
        安卓:layout_marginTop="40dp"
        安卓:background="#0000ff"
        安卓:text="proceed"
        安卓:textColor="#ffffff"
        安卓:textSize="25dp" />

</RelativeLayout>

第一个测验。爪哇

package com.example.john.quiz1;

import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.widget.Button;
import 安卓.widget.RadioGroup;

import com.example.john.quiz1.R;

public class FirstQuiz extends AppCompatActivity {
    private RadioGroup radioGroup;
    private Button nxt;
    int pos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz1);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId){
                if (checkedId == R.id.radio1) {
                    setContentView(R.layout.final2);
                }
                else if (checkedId == R.id.radio2){
                    setContentView(R.layout.final2);
                }
                else if (checkedId == R.id.radio3) {
                    setContentView(R.layout.final1);
                }
                else{
                    setContentView(R.layout.final2);
                }
            }
        });
    }
}

共 (2) 个答案

  1. # 1 楼答案

    你的startActivityFromFragment方法应该在你的onClickListener()之外。这个方法也是空的,所以当您调用它时,它不会做任何事情

    看起来onCreateOptionsMenuonOptionsItemSelectedMainActivity类之外

  2. # 2 楼答案

    尝试在按钮xml中只添加startActivity(intent),还添加android:clickable=“true”和android:focusable=“true”

    我还注意到,您的startActivityFromFragment()方法不会做任何事情,因为您正在调用一个空方法,因此在该方法中,您应该启动intent