Android AlertBox应等待用户单击“确定”以导航到其他活动
Iam使用此代码在需要时显示警报Android AlertBox应等待用户单击“确定”以导航到其他活动,android,Android,Iam使用此代码在需要时显示警报 import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; impor
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginSettingsActivity extends Activity implements OnClickListener {
EditText ipAddr, port;
Button save, cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server_settings);
// Retrieving IP address and Port entered by user
ipAddr = (EditText) findViewById(R.id.edit_ip_address);
port = (EditText) findViewById(R.id.edit_portno);
// If Already IP and Port entered and stored in our cache, then bring
// that and display it
String ipAddress = ((GlobalClass) LoginSettingsActivity.this
.getApplication()).getServletIPAddress();
String portno = ((GlobalClass) LoginSettingsActivity.this
.getApplication()).getServletPort();
if (ipAddress != null && ipAddress.length() > 0)
ipAddr.setText(ipAddress);
if (portno != null && portno.length() > 0)
port.setText(portno);
// After entering Servlet IP and port, submit action handled here. Just
// storing IP and port in a global storage
save = (Button) findViewById(R.id.save_server_settings);
cancel = (Button) findViewById(R.id.cancel_server_settings);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View currentButton) {
switch (currentButton.getId()) {
case R.id.save_server_settings:
/* EditText ipAddress = (EditText) findViewById(R.id.edit_ip_address);
EditText portNo = (EditText) findViewById(R.id.edit_portno);*/
//checking if ipadress and port is null or not
if (ipAddr.length() <= 0 || port.length() <= 0) {
// LoginAlertDialog("Please make sure IP Address and Port are not empty!",
// false);
在这里,我正在同一个活动中创建警报对话框,显示对话框,但在按下“确定”键并导航到另一个活动之前,它会自动关闭
AlertDialog.Builder builder = new AlertDialog.Builder(LoginSettingsActivity.this);
builder.setTitle("Settings").setMessage("IP and Port set success!").setCancelable(false)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(LoginSettingsActivity.this,
LoginValidation.class);
// this addflags should be added, because we are
// starting new activity here from outside of the
// acutal caller activity.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
dialog.dismiss();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
case R.id.cancel_server_settings:
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
全局类只是一个要设置和获取的pojo类。我尝试从pojo类调用alert dialog,我在同一活动中也使用了alerrt dialog,但两者似乎都不起作用,对话框关闭,我收到窗口泄漏消息
我有一个登录页面,如果用户没有输入用户名和密码,则此警报将弹出,直到用户单击“确定”。它不会消失,因为我需要保持在同一页面中。(代码正常工作警报框会一直保持,直到用户单击“确定”)
相同的对话框iam调用“我的另一个活动”来保存一些全局值,单击“保存”后,将显示相同的对话框,并应等待用户单击“确定”,因为只有在用户单击“确定”时,它才应导航到另一个活动
AlertDialog.Builder builder = new AlertDialog.Builder(LoginSettingsActivity.this);
builder.setTitle("Settings").setMessage("IP and Port set success!").setCancelable(false)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(LoginSettingsActivity.this,
LoginValidation.class);
// this addflags should be added, because we are
// starting new activity here from outside of the
// acutal caller activity.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
dialog.dismiss();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
case R.id.cancel_server_settings:
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
但是在这里,如果没有自动等待,它将导航到另一个活动问题得到解决,只是一个简单的错误,忘记使用断开开关状态 这里是修改后的代码
@Override
public void onClick(View currentButton) {
switch (currentButton.getId()) {
case R.id.save_server_settings:
/* EditText ipAddress = (EditText) findViewById(R.id.edit_ip_address);
EditText portNo = (EditText) findViewById(R.id.edit_portno);*/
//checking if ipadress and port is null or not
if (ipAddr.length() <= 0 || port.length() <= 0) {
// LoginAlertDialog("Please make sure IP Address and Port are not empty!",
// false);
((GlobalClass) LoginSettingsActivity.this.getApplication())
.SettingsAlertDialog(
"Please make sure IP Address and Port are not empty!",
LoginSettingsActivity.this, false, null, null);
} else {
//Getting ipaddress from the user and setting it in the GlobalClass
((GlobalClass) LoginSettingsActivity.this.getApplication())
.setServletIPAddress(ipAddr.getText().toString());
//Getting portno from the user and setting it in the GlobalClass
((GlobalClass) LoginSettingsActivity.this.getApplication())
.setServletPort(port.getText().toString());
System.out.println("Inside setting IP and port "
+ ipAddr.getText().toString() + " "
+ port.getText().toString());
// LoginAlertDialog("IP and Port set success!", true);
/* ((GlobalClass) LoginSettingsActivity.this.getApplication())
.SettingsAlertDialog("IP and Port set success!",
LoginSettingsActivity.this, true,
LoginSettingsActivity.this,
LoginValidation.class);*/
AlertDialog.Builder builder = new AlertDialog.Builder(LoginSettingsActivity.this);
builder.setTitle("Settings").setMessage("IP and Port set success!").setCancelable(false)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(LoginSettingsActivity.this,
LoginValidation.class);
// this addflags should be added, because we are
// starting new activity here from outside of the
// acutal caller activity.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
dialog.dismiss();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
**break;**
case R.id.cancel_server_settings:
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
@覆盖
公共void onClick(查看当前按钮){
开关(currentButton.getId()){
案例R.id.save_服务器_设置:
/*EditText ip地址=(EditText)findViewById(R.id.edit\u ip\u地址);
EditText端口号=(EditText)findViewById(R.id.edit\u端口号)*/
//检查iPAddress和port是否为null
如果(ipAddr.length(),请发布您的完整代码…我认为活动是从其他地方开始的…请检查我是否已编辑,我收到了窗口泄漏的消息