如何访问放置在为 AlertDialog 制作的自定义布局中的 EditText?
How to access EditText placed in a custom layout made for an AlertDialog?
我为 AlertDialog
制作了一个自定义布局,其中有一个 EditText
。
点击一个按钮我想检查这个edittext是空的还是填充的,以便我可以进行进一步的操作。
这里是 xml AlertDialog 自定义布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type here"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
这是我使用 java 代码所做的:
final AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setTitle("Choose a unique username");
builder.setView(R.layout.choose_unique_name_dialog);
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (uniqueUserName.getText().toString().isEmpty()) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Please choose a unique username", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
signingUpMethod();
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
所以,我想知道如何从我的 MainActivity
访问此 EditText
?
请告诉我。
像这样为您的 alertDialog 设置布局
LayoutInflater inflater = this.getLayoutInflater();
View alertDialogView = inflater.inflate(R.layout.your_layout,null);
像这样访问您的 EditText
EditText editText = (EditText) alertDialog.findViewById(R.id.id);
试试这个代码,
将其设为 public
static
方法,您可以从任何其他 activity
访问
将 edittext
值存储在某些 static 变量中并从另一个 class.
访问
我为 AlertDialog
制作了一个自定义布局,其中有一个 EditText
。
点击一个按钮我想检查这个edittext是空的还是填充的,以便我可以进行进一步的操作。
这里是 xml AlertDialog 自定义布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type here"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
这是我使用 java 代码所做的:
final AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setTitle("Choose a unique username");
builder.setView(R.layout.choose_unique_name_dialog);
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (uniqueUserName.getText().toString().isEmpty()) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Please choose a unique username", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
signingUpMethod();
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
所以,我想知道如何从我的 MainActivity
访问此 EditText
?
请告诉我。
像这样为您的 alertDialog 设置布局
LayoutInflater inflater = this.getLayoutInflater();
View alertDialogView = inflater.inflate(R.layout.your_layout,null);
像这样访问您的 EditText
EditText editText = (EditText) alertDialog.findViewById(R.id.id);
试试这个代码,
将其设为 public
static
方法,您可以从任何其他 activity
将 edittext
值存储在某些 static 变量中并从另一个 class.