如何在 Dialog onClick 事件中访问 'findViewById' 方法
How to access 'findViewById' method inside Dialog onClick event
我有一个名为 login_form 的自定义布局 xml 文件和一个名为 login_form_root
的 LinearLayout
根文件。我正在尝试使用对话框显示此布局,但无法从 EditText
获取值,因为我无法在 onCreateDialog.
中解析方法 findViewById
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.login_form, null));
builder.setPositiveButton(okText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
View myView = inflater.inflate(R.layout.login_form, (ViewGroup) findViewById(R.id.login_form_root));
EditText userEmail = (EditText) myView.findViewById(R.id.email_address);
userEmailValue = userEmail.getText().toString();
Toast.makeText(getActivity(), userEmailValue, Toast.LENGTH_SHORT).show();
}
错误 在这里:-
"(ViewGroup) findViewById(R.id.login_form_root)"
那么如何扩展自定义布局以获得 EditText 值?
你应该在膨胀它的时候保存它,然后你可以在以后使用它
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
final View view = layoutInflater.inflate(R.layout.login_form, null);
builder.setView(view);
AlertDialog dialog = builder.create();
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText userEmail = (EditText) view.findViewById(R.id.email_address);
String userEmailValue = userEmail.getText().toString();
}
});
我有一个名为 login_form 的自定义布局 xml 文件和一个名为 login_form_root
的 LinearLayout
根文件。我正在尝试使用对话框显示此布局,但无法从 EditText
获取值,因为我无法在 onCreateDialog.
findViewById
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.login_form, null));
builder.setPositiveButton(okText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
View myView = inflater.inflate(R.layout.login_form, (ViewGroup) findViewById(R.id.login_form_root));
EditText userEmail = (EditText) myView.findViewById(R.id.email_address);
userEmailValue = userEmail.getText().toString();
Toast.makeText(getActivity(), userEmailValue, Toast.LENGTH_SHORT).show();
}
错误 在这里:-
"(ViewGroup) findViewById(R.id.login_form_root)"
那么如何扩展自定义布局以获得 EditText 值?
你应该在膨胀它的时候保存它,然后你可以在以后使用它
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
final View view = layoutInflater.inflate(R.layout.login_form, null);
builder.setView(view);
AlertDialog dialog = builder.create();
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText userEmail = (EditText) view.findViewById(R.id.email_address);
String userEmailValue = userEmail.getText().toString();
}
});