如何在单击按钮上调用活动?

时间:2013-08-12 15:09:37

标签: java android button android-activity

我通过单击按钮调用活动。我把我的代码放在这里。所以问题是,当我在规则按钮上调用我的规则活动时,单击它会调用它,但它也会调用其他选项按钮。

public class Main extends Activity implements OnClickListener{
/** the button that I've added in the xml*/
Button options_btn;
Button Exit_label;
Button player_data_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {

       /** Called when the activity is first created. */

         super.onCreate(savedInstanceState);
         setContentView(R.layout.cyk_main);

    /** Get the id of the button from the xml*/
    options_btn = (Button) findViewById(R.id.options_btn);
    options_btn.setOnClickListener(this);

    /** Get the id of the button from the xml*/
    player_data_btn = (Button) findViewById(R.id.player_data_btn);
    player_data_btn.setOnClickListener(this);
}

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

    /** make a refernce to store the intent when the view has been clicked*/
    Intent intent;
    Intent intent1;
    /** Make cases according to the number of buttons you have in screen
     * In this case, I've added one.*/
    switch(view.getId()){

    case R.id.options_btn :
        Log.i("btnClick","Start button id is"+view.getId());
        /** Intent should be fired from this activity to the other activity*/
        intent = new Intent(Main.this, SoundLayout.class);
        /** This is useful when you want to get the button that is clicked here on the 
         * destination activity*/
        intent.putExtra("button_click", "Start");
        /** Start the intent*/
        startActivity(intent);

    case R.id.player_data_btn :
        Log.i("btnClick","Start button id is"+view.getId());
        /** Intent should be fired from this activity to the other activity*/
      intent1 = new Intent(Main.this, RulesActivity.class);
        intent1.putExtra("button_click", "Start");
        /** Start the intent*/
        startActivity(intent1);


        /*** Use this only when you want to finish your current activity while opening an another one.
         * if this is commented, then this activity will be running in the background.
        ***/
        this.finish();
        break;
    }


     Button Exit_label=(Button)findViewById(R.id.Exit_label);
     Exit_label.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
     // TODO Auto-generated method stub
     AlertDialog.Builder builder=new AlertDialog.Builder(Main.this);
     //Set a title
     builder.setTitle("Exit?");
     //Set a message
     builder.setMessage("Want to Exit?");
     builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
     //Displaying a toast message
     Toast.makeText(getApplicationContext(), "Your Game Exit", Toast.LENGTH_LONG).show();
     finish();
     System.exit(0);
     }
     });
     builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
     dialog.cancel();

     }
     });

     //Create the dialog
     AlertDialog alertdialog=builder.create();
     //show the alertdialog
     alertdialog.show();
     }
     });

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

在这里,当我调用我的规则时,它也会出现在选项按钮上。那我怎么解决这个问题呢?

2 个答案:

答案 0 :(得分:2)

您没有提到第一个案例陈述的break语句。 添加它将起作用的休息..

答案 1 :(得分:0)

必须放置break statement at the end of every case。在第一个案例结束时你是missing the break statement。因此,如果第一种情况为真,则一旦执行第一种情况,由于第一种情况结束时缺少break语句,第二种情况也将被执行。这导致了这个问题。

switch(view.getId()){

case R.id.options_btn :
    ....
     break; // you are missing this
case R.id.player_data_btn :
    ....
    break;
}