Inf*_*izz 8 android android-widget android-edittext android-alertdialog
我把头发拉过这一头.
我有一个应用程序,当您按下菜单项时,我希望它显示输入警报对话框.当用户点击"确定"时,他们输入到对话框中的EditText的文本想要返回以便稍后在活动中使用.
所以我想:
name = input.getText().toString(); //input is an EditView which is the setView() of the dialog
Run Code Online (Sandbox Code Playgroud)
在"确定"按钮的onClick内部可以工作,但事实并非如此.Eclipse告诉我,我无法在onClick中设置名称,因为它不是最终的,但如果我将名称的定义更改为final,则无法明显更改,因此无法在onClick()中设置.
以下是此位的完整代码:
String routeName = "";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText inputName = new EditText(this);
alert.setView(inputName);
alert.setPositiveButton("Set Route Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
routeName = inputName.getText().toString();
}
});
alert.show();
Run Code Online (Sandbox Code Playgroud)
我必须在这里做一些非常愚蠢的事情因为我已经搜索了很长时间并且发现没有其他人有同样的问题.
有谁能请赐教?
感谢您的时间,
InfinitiFizz
事实上,您无法访问 中的任何内容onClick,我认为这是一种匿名方法。你能做的就是了解背景并从中找到你的观点。它看起来像这样:
yourButton.setOnClickListener(new View.OnClickListener() {
public boolean onClick(View v) {
name = findViewById(R.id.yourInputId)).getText().toString();
return true;
}
);
Run Code Online (Sandbox Code Playgroud)
您还可以使用该v参数,例如通过调用v.getContext()侦听器。