26

ポジティブ ボタンとネガティブ ボタンを備えた AlertDialog があります。AlertDialog レイアウトには、EditText と 2 つのボタン (btnAdd1、btnAdd2) があります。ユーザーがボタン btnAdd1 または btnAdd2 をクリックすると、AlertDialog の EditText に同じテキストが追加されます (ただし、AlertDialog は閉じません)。これは AlertDialog で可能ですか、それともダイアログのみを使用する必要がありますか?

これは AlertDialog のレイアウト (R.layout.prompt) です。

<LinearLayout>
<EditText
    android:id="@+id/userInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnAdd1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bla" />

<Button
    android:id="@+id/btnAdd2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bla" />

  </LinearLayout>

そして、これはソースコードです:

    LayoutInflater layoutInflater = LayoutInflater.from(this);
        View promptView = layoutInflater.inflate(R.layout.prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(promptView);
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                              //...

                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    AlertDialog alertD = alertDialogBuilder.create();
    alertD.show();

レイアウトから btnAdd1 と btnAdd2 にアクセスしたい。OnClickListener() をこれら 2 つのボタンに設定します。

4

6 に答える 6

3

このアプローチによれば、私は画像ボタンを作成することができますが、キャンセルボタンのダイアログを閉じるかキャンセルしたい場合は、どうすればよいですか..

public static void alertDialogShow(final Context context,
            final String resultMobile) {

        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.prompt,
                null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);
        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.editTextDialogUserInput);
        userInput.setText(resultMobile);
        userInput.setEnabled(false);
btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

            }
        });
于 2015-08-22T06:49:07.633 に答える