【发布时间】:2012-12-03 19:16:33
【问题描述】:
我正在尝试根据先前的输入提示用户输入。
请允许我澄清并添加一些上下文:
此应用程序是一个掷骰子模拟器。主活动显示每个骰子上的骰子数和面数,可通过从菜单中打开子活动来更改。使用startActivityForResult() 调用此子活动以获取其结果,它完全由RadioGroup 和两个按钮 - “接受”和“取消” - 并将String 额外传递回主活动(格式为“XdN” - 例如“1d20”),然后主要活动对其进行解析并用于相应地调整骰子数和边数。
这个系统在“1d6”、“1d10”等示例中运行得非常好,并且不是问题 - 据我所知。
现在,这是我的问题:
我有一个通过String“自定义”的单选按钮(而不是类似于“XdN”的String)。选择后(最后,当单击“接受”时),我的意图是打开一个对话框并提示用户输入自定义计数值 - 这有点工作。对话框显示,布局是正确的,看起来应该是正确的,但是 布局会在瞬间消失,并传递当前计数值而不是 应该输入的值由用户。 这也会向 LogCat(我使用的是 Eclipse 和 ADT)抛出一个错误,上面写着:
Application Tag Text
org.x3chaos.nicedice WindowManager Activity org.x3chaos.nicedice.DiceActivity
has leaked window com.android.internal.pol
icy.impl.PhoneWindow$DecorView@44786f20 th
at was originally added here
org.x3chaos.nicedice WindowManager android.view.WindowLeaked: Activity org.x3
chaos.nicedice.DiceActivity has leaked win
dow com.android.internal.policy.impl.Phone
Window$DecorView@44786f20 that was origina
ly added here
org.x3chaos.nicedice WindowManager at android.view.ViewRoot.<init>(ViewRoot.j
ava:247)
[more if needed]
--
Button button_accept 的android:onClick 方法如下:
public void accept(View view) {
RadioGroup group = (RadioGroup) findViewById(R.id.dice_radiogrp);
RadioButton selected = (RadioButton) findViewById(group
.getCheckedRadioButtonId());
String tmpResult = selected.getText().toString();
if (!tmpResult.matches("[0-9|xX](.*)[Dd][0-9](.*)")) {
// Show dialog
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.prompt_dice, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Custom Dice");
builder.setCancelable(false);
final EditText editCount = (EditText) findViewById(R.id.edit_promptDiceCount);
final EditText editSides = (EditText) findViewById(R.id.edit_promptDiceSides);
builder.setPositiveButton("Accept", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean flag = false;
String textCount, textSides;
textCount = editCount.getText().toString();
if (textCount.equals("")) {
textCount = "1";
flag = true;
}
textSides = editSides.getText().toString();
if (textSides.equals("")) {
textSides = "20";
flag = true;
}
int count = Integer.parseInt(textCount);
int sides = Integer.parseInt(textSides);
if (count > 20)
count = 20;
if (sides > 20)
sides = 20;
String finRes = count + "d" + sides;
setResultExtra(finRes);
if (flag) {
String msg = "Invalid input: set dice to " + finRes;
Toast toast = Toast.makeText(context, msg,
Toast.LENGTH_SHORT);
toast.show();
}
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
exitActivity(false);
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
result = tmpResult;
}
exitActivity(true);
}
所有其他功能都可以工作,所以我不确定为什么不这样。我确定这是我搞砸的事情 - 因为这是我的第一个应用程序,我正在使用一些我以前从未接触过的对象和方法 - 所以我非常感谢任何帮助。 :]
编辑:阅读this question 后,我开始认为可能是Intent 没有等待对话完成。但这不太有意义,因为调用对话框的 Activity 会被调用以获得结果。
【问题讨论】:
标签: android dialog android-alertdialog adk