对,我知道有很多类似的问题。我研究过关于这个的stackoverflow以及在互联网上,但仍然难倒。从AlertDialog Builder中的EditText获取值
此代码位于一个片段中。
...
private Context context = getActivity();
public void Dialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View mView = inflater.inflate(R.layout.dialog, null);
alertDialogBuilder.setView(mView);
EditText a = (EditText) mView.findViewById(R.id.a);
EditText b = (EditText) mView.findViewById(R.id.b);
EditText c = (EditText) mView.findViewById(R.id.c);
//a.setText("abc");
//b.setText("xyz");
//c.setText("123");
strA = a.getText().toString();
strB = b.getText().toString();
final String strC = c.getText().toString();
}
这应该是越来越膨胀的布局的视图,并用它来访问视图中的元素的典型方法,但它不工作不管我试过了,我只是不能让STRA,STRB和strC值使用getText()。toString()。
//a.setText("abc");
//b.setText("xyz");
//c.setText("123");
但是,如果我取消注释上述3行,值会发送。我可以在strA,strB,strC中接收它们。这是为什么?我一点都不明白。
任何帮助非常感谢,谢谢!
没有任何操作从EditTexts获取字符串。您应该在对话框中定义一个按钮。比你可以通过button.onclicklistener得到的字符串... –
是的谢谢你的建议。我完全忽略了这一点。事实上,我在这个代码下面有一个onClickListener,我正在检查值。业余/累了的错误。谢谢! – lyk13