我是 java 编程新手,我正在创建一个简单的应用程序,在一个活动中包含多个按钮。我遇到的问题是按钮只能按顺序工作,一个按钮只有在按下另一个按钮后才能执行其工作,任何帮助将不胜感激。下面的代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Settings button start
Button settingsButton = (Button) findViewById(R.id.btnSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startSettings();
}
});
}
public void startSettings() {
Intent launchSettings = new Intent(this, SettingsScreen.class);
startActivity(launchSettings);
// Settings button end
// Set A Button start
Button setAButton = (Button) findViewById(R.id.btnSetA);
setAButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
setzoneA();
}
});}
public void setzoneA(){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
// Set A Button end
// Set B Button start
Button setBButton = (Button) findViewById(R.id.btnSetB);
setBButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
setzoneB();
}
});}
public void setzoneB(){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
}
// Set B Button end
@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;
}
}