[英]How to getText() from a TextView in an AlertDialog?
I have an AlertDialog that opens pressing a Button. 我有一个AlertDialog,可以通过按下Button打开。
In this AlertDialog there are a button and a TextView showing a number. 在此AlertDialog中,有一个按钮和一个显示数字的TextView。
I have to create a function that increments by 1 the number in the TextView when the button in the AlertDialog is pressed. 我必须创建一个函数,当按AlertDialog中的按钮时,TextView中的数字增加1。
In order to do that, I wrote this into the .java file of the activity that opens the AlertDialog. 为了做到这一点,我将其写入打开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));
}
But it gives error on total.getText() 但这给了total.getText()错误
I tried to write something similar, but with the TextView into the activity, and it works fine. 我尝试编写类似的内容,但将TextView插入活动中,并且工作正常。
I started programming Android a week ago, I'm not very good. 我一周前开始进行Android编程,但我不是很好。 Please, help me! 请帮我!
Thank you! 谢谢!
If your dialog variable is named diag, then try the following 如果您的对话框变量名为diag,请尝试以下操作
TextView total = (TextView) diag.findViewById(R.id.Total);
Notice that you are calling the findViewById()
on the Dialog
not the Activity
请注意,您是在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.