[英]How to get a text from EditText?
I make DialogFragment
with custom layout
and set this such way 我用自定义layout
制作DialogFragment
并以这种方式设置
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
.setView(layout)
.setCancelable(true);
This layout
which was set in DialogFragment
contains EditText
. 这种layout
这是在设置DialogFragment
包含EditText
。 Gist in fact that when DialogFragment
appear, user have to enter in EditText
your comment and then push button send
. 要点在于,当DialogFragment
出现时,用户必须在EditText
输入您的注释,然后按send
按钮。
It was a problem when i tried findViewById()
, because DialogFragment
didn't extend Activity
, thus i try solve this issue such way : 当我尝试findViewById()
,这是一个问题,因为DialogFragment
没有扩展Activity
,因此我尝试通过以下方式解决此问题:
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_note_dialog, null);
EditText etNoteFromWeb = (EditText)view.findViewById(R.id.etNoteFromWeb);
String response = etNoteFromWeb.getText().toString();
But etNoteFromWeb.getText().toString();
但是etNoteFromWeb.getText().toString();
everytime give me back empty line, not null
... Actually empty line. 每次给我回空行,不为null
...实际上是空行。
What i am doing wrong? 我做错了什么?
Here is full code of DialogFragment
: 这是DialogFragment
完整代码:
public class MyDialog extends DialogFragment { 公共类MyDialog扩展DialogFragment {
final String LOG_TAG = "MyDialog";
private static MyDialog myDialog;
private EditText etNoteFromWeb;
int layout;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
Bundle args = this.getArguments();
layout = args.getInt("layout");
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
.setView(layout)
.setCancelable(true);
if (layout == R.layout.activity_note_dialog) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(layout, null);
LinearLayout llSent = (LinearLayout) view.findViewById(R.id.llSent);
LinearLayout llSkip = (LinearLayout) view.findViewById(R.id.llSkip);
llSent.setOnClickListener(listener);
llSkip.setOnClickListener(listener);
adb.setView(view);
}
return adb.create();
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (layout == R.layout.activity_note_dialog) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_note_dialog, null);
etNoteFromWeb = (EditText) view.findViewById(R.id.etNoteFromWeb);
String response = etNoteFromWeb.getText().toString();
// String response = "etNoteFromWeb.getText().toString()";
// Здесь в System.out.println ничего нет...9(( но когда раскоментирована строка
// выше и указываю статический текст, то все работает, проблема именно в том,
// что не достает текст из EditText
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + response);
switch (v.getId()) {
case R.id.llSent:
onClick.onDialogClickListener(response);
myDialog.dismiss();
break;
case R.id.llSkip:
onClick.onDialogClickListener(null);
myDialog.dismiss();
break;
}
}
}
};
public void dismissDialog(final MyDialog dialog, int daleyTime) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
dialog.dismiss();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}, daleyTime);
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
Log.d(LOG_TAG, "MyDialog: onDismiss");
}
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
Log.d(LOG_TAG, "MyDialog: onCancel");
}
public static MyDialog newInstance(int layout) {
myDialog = new MyDialog();
Bundle args = new Bundle();
args.putInt("layout", layout);
myDialog.setArguments(args);
return myDialog;
}
// Этот интерфейс я реализовал для того чтоб можно было передавать данные из DialogFragment
public interface OnButtonClick {
void onDialogClickListener(String response);
}
OnButtonClick onClick;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onClick = (OnButtonClick) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onDialogClickListener");
}
}
@Override
public void onDetach() {
super.onDetach();
onClick = null;
}
}
This is because you re-inflate your layout in onClick
, so the text is always empty. 这是因为您在onClick
重新增加了布局,因此文本始终为空。 You should move the line 你应该移动线
etNoteFromWeb = (EditText) view.findViewById(R.id.etNoteFromWeb);
inside onCreateDialog
to make it work. 在onCreateDialog
内使其工作。
you creating new view and get string from it!! 您创建新视图并从中获取字符串!! its not dialog's view use : 它不是对话框的视图用途:
EditText etNoteFromWeb = (EditText)dialog.findViewById(R.id.etNoteFromWeb);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.