我在用户启动App时启动警报对话框,如下所示:
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
启动另一个Dialog,询问另一组身体信息。
答案 0 :(得分:1)
试试这个
而不是使用
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();
}
答案 1 :(得分:1)
看看this。
首先你创建构建器,添加按钮,视图等然后你调用AlertDialog dialog = builder.create();
我可以看到onClick回调甚至没有被执行。