[英]How to getText() from a TextView in an AlertDialog?
我有一個AlertDialog,可以通過按下Button打開。
在此AlertDialog中,有一個按鈕和一個顯示數字的TextView。
我必須創建一個函數,當按AlertDialog中的按鈕時,TextView中的數字增加1。
為了做到這一點,我將其寫入打開AlertDialog的活動的.java文件中。
public void plus(View view)
{
TextView total = (TextView) findViewById(R.id.Total);
totalP = Integer.parseInt((String)(total.getText())) + 1;
total.setText(String.valueOf(totalP));
}
但這給了total.getText()錯誤
我嘗試編寫類似的內容,但將TextView插入活動中,並且工作正常。
我一周前開始進行Android編程,但我不是很好。 請幫我!
謝謝!
如果您的對話框變量名為diag,請嘗試以下操作
TextView total = (TextView) diag.findViewById(R.id.Total);
請注意,您是在Dialog
而不是Activity
上調用findViewById()
final EditText input = new EditText(MainActivity.this);
input.setSingleLine(true);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Title")
.setView(input)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String in = input.getText().toString();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.