如何从此自定义警报对话框中禁用/启用肯定按钮?

时间:2019-06-11 18:39:30

标签: android android-fragments dialog

我已经建立了一个带有自定义视图的Alertdialog,但是在获取肯定和否定按钮时遇到了麻烦。我已经使用alertdialog.builder创建了对话框,但是在dialog.create之后,.getButton()函数不可用。构建对话框时我做错了什么,还是有另一种方法来解决这个问题?

我正在尝试创建一个记分板应用程序,该应用程序的开始屏幕显示已保存游戏的列表,并带有一个浮动操作按钮以创建一个新游戏。单击它时,将出现一个对话框,用户必须在其中输入游戏标题和2位玩家的名称(还有更多要添加的名称)。单击“确定”(对话框的肯定按钮)时,应用程序将显示一条吐司消息,其中包含游戏标题和玩家名称,稍后我将通过创建一个新的“游戏”对象来替换此消息。

这是显示在对话框中的视图:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:animateLayoutChanges="true">


<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/txt_NewGame_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_nameofnewgame"
        android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/txt_NewGame_Player1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/Player1"
        android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout3">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/txt_NewGame_Player2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/Player2"
        android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

</android.support.constraint.ConstraintLayout>`

单击fab后,将调用此函数:

public void newGame(){
    //Show dialog to make a new game
    DialogNewGame dialogNewGame = new DialogNewGame();
    dialogNewGame.show(getSupportFragmentManager(),"dialog new game");
}

这是我的对话框的类(活动?片段活动?):

public class DialogNewGame extends AppCompatDialogFragment {

private TextInputEditText editText_title;
private TextInputEditText editText_namePlayer1;
private TextInputEditText editText_namePlayer2;

private DialogNewGameListener listener;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());

    LayoutInflater inflater = requireActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_newgame, null);

    builder.setTitle(R.string.title_newgame)
            .setView(dialogView)
            .setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String gameTitle = editText_title.getText().toString();
                    String namePlayer1 = editText_namePlayer1.getText().toString();
                    String namePlayer2 = editText_namePlayer2.getText().toString();
                    listener.applyInput(gameTitle, namePlayer1, namePlayer2);

                }
            })
            .setNegativeButton(R.string.back, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //ANNULEREN
                }
            });

    editText_title = dialogView.findViewById(R.id.txt_NewGame_Name);
    editText_namePlayer1 = dialogView.findViewById(R.id.txt_NewGame_Player1);
    editText_namePlayer2 = dialogView.findViewById(R.id.txt_NewGame_Player2);

    return builder.create();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        listener = (DialogNewGameListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + "must implement DialogNewGameListener");
    }
}

public interface DialogNewGameListener {
    void applyInput(String gameTitle, String namePlayer1, String namePlayer2);
}

这是我的期望:对话框类使用alertdialog.builder在创建新的dialogNewGame()时返回一个对话框,在此对话框上,我可以使用来获得肯定按钮 dialognewgame.getButton(AlertDialog.BUTTON_POSITIVE) 我不能打电话给 getButton() 功能在创建的对话框上。

0 个答案:

没有答案