我的问题是如何获取用户输入到我的EditText
中的数据,然后使用Toast.makeTest
方法显示数据。
我不知道如何访问片段中的EditText
而且此链接对我不起作用:
getText from a EditText in a DialogFragment
这是我的代码:
public class FragmentDialog extends DialogFragment {
EditText entry;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.alert_dialog).setTitle("Add your text").setMessage("HElllloooooooo").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
答案 0 :(得分:0)
您必须在onCreateView上找到如下所示的ViewbybyId
public class FragmentDialog extends DialogFragment {
EditText entry;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_username, container);
entry = (EditText) view.findViewById(R.id.username);
mEditText.requestFocus();
return view;
}}
答案 1 :(得分:0)
编辑文本的工作方式:
首先,您应该将其绑定:
entry = view.findViewById(R.id.MyEdtText);
第二个要在编辑中访问书面数据(文本),您应该调用:
entry.getText().toString()
要在Toast中显示书面文字,您必须:
if(TextUtils.isEmpty(entry.getText().toString()){
Toast.makeText(getActivity(), "No Text Entered", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity(), "The Text Entered Is"+ entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
针对您的案例的具体解决方案是:
public class FragmentDialog extends DialogFragment {
private EditText entry;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogView = inflater.inflate(R.layout.alert_dialog,null);
entry = DialogView.findViewById(R.id.entryEditText); // replace it with the correct XML ID
builder.setView(DialogView).setTitle("Add your text").setMessage("HElllloooooooo").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "canceled operation", Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "the edit text value: "+entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
要显示该对话框,请在您的活动中调用以下代码:
FragmentDialog dialog = new FragmentDialog();
dialog.show(getSupportFragmentManager(), "YourDialog");
概念验证(查看其工作原理):