按钮单击功能不起作用

时间:2014-05-25 18:30:57

标签: java android xml

我试图按下按钮并转到下一个活动,但我的代码无效。我的java和xml代码在这里。

case R.id.menu_legalnotes:
       startActivity(new Intent(this, LegalNoticeActivity.class));
        return true;


    <Button
    android:id="@+id/menu_legalnotes"
    android:title="@string/legalnotes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_toRightOf="@+id/floors_fragment"
    android:text="Legal Notice" />

3 个答案:

答案 0 :(得分:0)

你做了:findViewById(R.id.menu_legalnotes).setOnClickListener(this); ??

答案 1 :(得分:0)

在您的活动中获取按钮的身份号

在onCreate方法中声明如下:

Button menuLegalNotes = (Button)findViewById(R.id.menu_legalnotes);

menuLegalNotes.setOnClickListner(new View.onClickListener){

@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
});
}

答案 2 :(得分:0)

以下是解决方案:

<强> 1) 在你的onCreate(){ 。 。

public class TestClass extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button yourButton = (Button)findViewById(R.id.menu_legalnotes);
        //If you have more buttons,
        Button secondButton = (Button)findViewById(R.id.secondButtonID);
        //Register more buttons if you have.

    }

<强> 2) onClick(查看v):

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

         switch (v.getId()) {
            case R.id.menu_legalnotes:

                Intent intent = new Intent(MainActivity.this, Your_SecondActivity.class);
                startActivity(intent);
                break;


            case R.id.secondButtonID:
                Intent intent = new Intent(MainActivity.this, Your_AnotherActivity.class);
                startActivity(intent);
                break;
         //You can add more cases here depending on what you want to achieve.

            default:
                break;
            }

    }

现在应该工作..很享受...... :)。