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;
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);
这里我从pojo类获取警告对话框
((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();
}
case R.id.cancel_server_settings:
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
Global class只是一个设置和获取的pojo类。我尝试从pojo类调用警告对话框,我也在相同的活动中使用了alerrt对话框,但两者似乎都不起作用,对话框关闭,我得到窗口泄露消息。
如果用户不是用户名和密码,那么我有一个登录页面然后这个警告弹出,直到用户点击确定它不会消失,因为我需要保持在同一页面。(代码工作正常警报框保持到用户点击确定)我在另一个活动中调用同一个对话框以保存一些全局值,单击“保存”后会出现相同的对话框,它应该等到用户单击“确定”,因为只有当用户单击“确定”时才会导航到另一个活动。
但是这里没有自动等待,它会导航到另一个活动
答案 0 :(得分:0)
问题解决了,只是一个简单的错误,忘记在switch case语句中使用break,这里修改后的代码
@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();
}
}