Java 按钮一个接一个地工作,而不是同时工作
我是java编程新手,我正在创建一个简单的应用程序,在一个活动中有多个按钮。我遇到的问题是,按钮只能按顺序工作,一个按钮只能在按下另一个按钮后才能执行其工作,任何帮助都将不胜感激。 代码如下:Java 按钮一个接一个地工作,而不是同时工作,java,android,class,button,methods,Java,Android,Class,Button,Methods,我是java编程新手,我正在创建一个简单的应用程序,在一个活动中有多个按钮。我遇到的问题是,按钮只能按顺序工作,一个按钮只能在按下另一个按钮后才能执行其工作,任何帮助都将不胜感激。 代码如下: import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.conten
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;
}
}您需要在onCreate()超级函数中设置所有onClick()侦听器。当前解决方案仅在单击“上一步”按钮时设置侦听器
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();
}
});
// Set A Button start
Button setAButton = (Button) findViewById(R.id.btnSetA);
setAButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
setzoneA();
}
});
// Set B Button start
Button setBButton = (Button) findViewById(R.id.btnSetB);
setBButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
setzoneB();
}
});
}
public void startSettings() {
Intent launchSettings = new Intent(this, SettingsScreen.class);
startActivity(launchSettings);
// Settings button end
}
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
}
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;
}
因为您为button have dependency设置了监听器,所以所有按钮启动和监听器都应该在onCreate()中
是的,这是因为您为第一个按钮的单击操作中的第二个按钮设置了单击侦听器。你需要在其他事件中设置onclicklisteners,通常在窗体/windows加载或创建事件中完成。我知道,我知道这是我的代码顺序错了,我正试图弄清楚应该如何订购。是的,这是可行的,非常感谢你的帮助!!我现在知道将来该做什么了
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();
}
});
Button setAButton = (Button) findViewById(R.id.btnSetA);
setAButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
setzoneA();
}
});
// Set B Button start
Button setBButton = (Button) findViewById(R.id.btnSetB);
setBButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
setzoneB();
}
});
}
public void startSettings() {
Intent launchSettings = new Intent(this, SettingsScreen.class);
startActivity(launchSettings);
}
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
}
});
}
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
}
});
}
@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;
}