它的一個非常愚蠢的問題,但我剛剛開始用java的android程序與幾天前的android,我不知道該怎麼辦....AlertDialog有兩個編輯 - 當按下按鈕確定 - >開始新的提示
所以我想要一個AlertDialog當你按下一個按鈕。此AlertDialog需要2個編輯。當你按下OK時,它必須計算一些東西,並在新的AlertDialog中顯示解決方案。
這就是我得到了什麼:
public void btn_own(View view) {
int a, b, c;
final String s;
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter range");
final EditText inputa = new EditText(this);
final EditText inputb = new EditText(this);
alert.setView(inputa);
alert.setView(inputb);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int aa, bb, cc , e ;
aa = new Integer(String.valueOf(inputa));
bb = new Integer(String.valueOf(inputb));
cc = bb - aa;
Random rand = new Random();
int d = rand.nextInt(cc);
e = d + bb;
Integer out = new Integer(e);
s = out.toString();
new AlertDialog.Builder(this)
.setMessage(s)
.show();
}
});
}
最後的 「本」 是錯誤的。我得到的消息:
Builder(android.content.Context) in Builder cannot be applied to (android.content.DialogInterface.OnClickListener)
,但我不知道我能寫的,而不是「這個」
一個另一個問題是,該「S」線條上「這個」被標記。 Cannot assign a value to final variable 's'
希望你能幫助我
編輯:
我的新代碼:
public void btn_own(View view) {
int a, b, c; // a : untere Grenze , b : obere Grenze
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter range");
final EditText inputa = new EditText(this);
final EditText inputb = new EditText(this);
alert.setView(inputa);
alert.setView(inputb);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int aa = 0;
int bb = 0;
int cc = 0;
int e = 0;
try {
aa = Integer.parseInt(inputa.getText().toString());
} catch (NumberFormatException f) {
System.out.println("not a number: " + inputa.getText().toString());
f.printStackTrace();
}
try {
bb = Integer.parseInt(inputb.getText().toString());
} catch (NumberFormatException g) {
System.out.println("not a number: " + inputb.getText().toString());
g.printStackTrace();
}
cc = bb - aa;
Random rand = new Random();
int d = rand.nextInt(cc);
e = d + bb;
Integer out = new Integer(e);
s = out.toString();
new AlertDialog.Builder(Decider.this)
.setMessage(s)
.show();
}
});
alert.show();
}
錯誤: 好了,現在它的工作原理是:我可以輸入一個號碼,然後我得到另一個隨機數。 但我怎麼能讓它工作如:我輸入一個範圍,我得到一個隨機數字? 它爲什麼只查看一個編輯?
感謝您的更新,謝謝你的更新,但我必須在最後一小時做一些東西 所以,我編輯我的代碼,但我仍然得到無效int ...我仍然只有一個編輯...) 沒有想要像我想要的工作..:D – QNikE
@ QNikE讓我們再試一次更新的代碼!;-),並在案件中打破了什麼將在日誌中? – donfuxx
OKK用的try/catch 檢查更新 問題是試了一下:AA和BB「可能尚未初始化」 ,我仍然有隻有一個編輯,但我需要兩個... – QNikE