0

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

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;
}   
}

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

4

2 回答 2

2

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

于 2013-08-12T15:11:39.383 回答
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;
}
于 2013-08-12T15:19:32.643 回答