无法关闭警报对话框 Android
Can't dismiss Alert Dialog Android
我在用户启动应用程序时启动一个警告对话框,如下所示:
public class MainActivity extends ActionBarActivity {
final Context context = this;
private SharedPreferences mPrefs;
private Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
mEditor = mPrefs.edit();
mEditor.apply();
boolean isUsedBefore = mPrefs.getBoolean("used",false);
//if(isUsedBefore==false){
//do nothing
//}else{
launchDialogSex();
//launchDialogBody();
//}
}
private void launchDialogSex() {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
final AlertDialog alert = alertDialogBuilder.create();
// set title
alertDialogBuilder.setTitle("Körperangaben - Geschlecht")
.setSingleChoiceItems(R.array.sex_array, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//save chosen sex
String[] sex = new String[]{"male","female"};
mEditor.putString("sex", sex[which]);
//don't open Dialog by next launch
mEditor.putBoolean("used", true);
mEditor.commit();
alert.dismiss();
//auto launch next Dialog
launchDialogBody();
}
});
alertDialogBuilder.show();
}
private void launchDialogBody() {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.text_entry, null);
//text_entry is an Layout XML file containing two text field to display in alert dialog
final EditText input1 = (EditText) textEntryView.findViewById(R.id.mass);
final EditText input2 = (EditText) textEntryView.findViewById(R.id.height);
final EditText input3 = (EditText) textEntryView.findViewById(R.id.age);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
//create Dialog
alert
.setTitle("Körperangaben")
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
Log.i("AlertDialog","TextEntry 3 Entered "+input3.getText().toString());
/* User clicked OK so do some stuff */
mEditor.putString("mass",input1.getText().toString());
mEditor.putString("height",input2.getText().toString());
mEditor.putString("age",input3.getText().toString());
mEditor.commit();
energieBedarf();
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
//show Dialog
alert.show();
}
public void energieBedarf(){
String sex = mPrefs.getString("sex","female");
String massString = mPrefs.getString("mass","65");
String heightString = mPrefs.getString("height","165");
String ageString = mPrefs.getString("age","25");
double mass = Double.parseDouble(massString);
double height = Double.parseDouble(heightString);
int age = Integer.parseInt(ageString);
if(sex.equals("female")){
double grundUmsatzFemale = 655.1 + (13.7 * mass) + (5 * height) - (6.8* age);
//TODO add PAL into calculations
double gesamtUmsatzFemale = (grundUmsatzFemale + (grundUmsatzFemale * (1.2-1)) ) + ((grundUmsatzFemale + (grundUmsatzFemale * (1.2-1)))*0.07);
Log.e("Ausgabe", String.valueOf(gesamtUmsatzFemale));
}else{
//Double grundUmsatzMale = 66.47 + (9.6 * Double.parseDouble(mass)) + (1.8*Double.parseDouble(height)) - (4.7* Integer.parseInt(age));
//Double gesamtUmsatzMale = (grundUmsatzMale + (grundUmsatzMale * (1.2-1)) ) + ((grundUmsatzMale + (grundUmsatzMale * (1.2-1)))*0.07);
}
}
即使我遵循了几个 similar (for example) 问题,这似乎对提问者有用,但我的警报对话框仍然没有关闭。
信息:方法在 onCreate
中调用,launchDialogBody
启动另一个对话框,要求提供另一组正文信息。
试试这个
而不是使用
alert.dismiss();
替换为
dialog.dismiss();
像这样
@Override
public void onClick(DialogInterface dialog, int which) {
//save chosen sex
String[] sex = new String[]{"male","female"};
mEditor.putString("sex", sex[which]);
//don't open Dialog by next launch
mEditor.putBoolean("used", true);
mEditor.commit();
dialog.dismiss();
//auto launch next Dialog
launchDialogBody();
}
看看this。
首先创建生成器,添加按钮、视图等然后调用AlertDialog dialog = builder.create();
据我所知,甚至没有执行 onClick 回调。
我在用户启动应用程序时启动一个警告对话框,如下所示:
public class MainActivity extends ActionBarActivity {
final Context context = this;
private SharedPreferences mPrefs;
private Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
mEditor = mPrefs.edit();
mEditor.apply();
boolean isUsedBefore = mPrefs.getBoolean("used",false);
//if(isUsedBefore==false){
//do nothing
//}else{
launchDialogSex();
//launchDialogBody();
//}
}
private void launchDialogSex() {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
final AlertDialog alert = alertDialogBuilder.create();
// set title
alertDialogBuilder.setTitle("Körperangaben - Geschlecht")
.setSingleChoiceItems(R.array.sex_array, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//save chosen sex
String[] sex = new String[]{"male","female"};
mEditor.putString("sex", sex[which]);
//don't open Dialog by next launch
mEditor.putBoolean("used", true);
mEditor.commit();
alert.dismiss();
//auto launch next Dialog
launchDialogBody();
}
});
alertDialogBuilder.show();
}
private void launchDialogBody() {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.text_entry, null);
//text_entry is an Layout XML file containing two text field to display in alert dialog
final EditText input1 = (EditText) textEntryView.findViewById(R.id.mass);
final EditText input2 = (EditText) textEntryView.findViewById(R.id.height);
final EditText input3 = (EditText) textEntryView.findViewById(R.id.age);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
//create Dialog
alert
.setTitle("Körperangaben")
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
Log.i("AlertDialog","TextEntry 3 Entered "+input3.getText().toString());
/* User clicked OK so do some stuff */
mEditor.putString("mass",input1.getText().toString());
mEditor.putString("height",input2.getText().toString());
mEditor.putString("age",input3.getText().toString());
mEditor.commit();
energieBedarf();
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
//show Dialog
alert.show();
}
public void energieBedarf(){
String sex = mPrefs.getString("sex","female");
String massString = mPrefs.getString("mass","65");
String heightString = mPrefs.getString("height","165");
String ageString = mPrefs.getString("age","25");
double mass = Double.parseDouble(massString);
double height = Double.parseDouble(heightString);
int age = Integer.parseInt(ageString);
if(sex.equals("female")){
double grundUmsatzFemale = 655.1 + (13.7 * mass) + (5 * height) - (6.8* age);
//TODO add PAL into calculations
double gesamtUmsatzFemale = (grundUmsatzFemale + (grundUmsatzFemale * (1.2-1)) ) + ((grundUmsatzFemale + (grundUmsatzFemale * (1.2-1)))*0.07);
Log.e("Ausgabe", String.valueOf(gesamtUmsatzFemale));
}else{
//Double grundUmsatzMale = 66.47 + (9.6 * Double.parseDouble(mass)) + (1.8*Double.parseDouble(height)) - (4.7* Integer.parseInt(age));
//Double gesamtUmsatzMale = (grundUmsatzMale + (grundUmsatzMale * (1.2-1)) ) + ((grundUmsatzMale + (grundUmsatzMale * (1.2-1)))*0.07);
}
}
即使我遵循了几个 similar (for example) 问题,这似乎对提问者有用,但我的警报对话框仍然没有关闭。
信息:方法在 onCreate
中调用,launchDialogBody
启动另一个对话框,要求提供另一组正文信息。
试试这个
而不是使用
alert.dismiss();
替换为
dialog.dismiss();
像这样
@Override
public void onClick(DialogInterface dialog, int which) {
//save chosen sex
String[] sex = new String[]{"male","female"};
mEditor.putString("sex", sex[which]);
//don't open Dialog by next launch
mEditor.putBoolean("used", true);
mEditor.commit();
dialog.dismiss();
//auto launch next Dialog
launchDialogBody();
}
看看this。
首先创建生成器,添加按钮、视图等然后调用AlertDialog dialog = builder.create();
据我所知,甚至没有执行 onClick 回调。