4

AlertDialogのPositive、Negative、Neutralボタンをテキストではなくドローアブルに設定しようとしています。

私はこれまでこれを使用して成功しています:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {...})
.setNeutralButton("Trash", new DialogInterface.OnClickListener() {...})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {...});

AlertDialog alert = builder.create();
alert.show();

Button button0 = alert.getButton(AlertDialog.BUTTON_POSITIVE);
button0.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_save), null, null, null);
button0.setText("");

Button button1 = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
button1.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_delete), null, null, null);
button1.setText("");

Button button2 = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
button2.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_close_clear_cancel), null, null, null);
button2.setText("");

ただし、これは回避策です。なぜなら、私は実際には事後にテキストを消去しているだけだからです。私の問題は、ある種のテキストを設定しないとボタンをインスタンス化できないように見えることです。

最初から「[blank_space]」を設定すると、画像が左に押された場合と同じ結果が得られます。同じ場所にnullまたは""を設定すると、ボタンがまったくない状態でAlertDialogが描画されます。あなたはそれがここの写真でどのように左に押すかを見ることができます:

写真

とにかく写真だけを使うことはありますか?私の単純な状況で翻訳を処理しようとするよりもはるかに良いでしょう。

4

5 に答える 5

1

独自のカスタムダイアログを作成するだけです!! レイアウトでを指定ImageButtonして、そのレイアウトでダイアログを作成できます。ポジティブ、ネガティブ、ニュートラルのボタンに近づく必要はありません。ここで、カスタムレイアウトでボタンの代わりにImageButtonを使用するだけの優れたチュートリアルを見つけることができます。

于 2012-07-09T07:22:19.590 に答える
1

AlertDialogは非推奨になりました。代わりにDialogFragmentsの使用を検討してください。より多くの制御と再利用が可能になります。これは、それを使用してカスタマイズする方法を示した優れたグーグルブログです。

于 2012-07-09T07:23:34.240 に答える
1

アンドロイドドキュメントから、

setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text.

画像を左側に配置しているため、ボタンの左側に画像が表示されます。あなたはそれを上/下に配置しようとするかもしれません。

もう1つの方法は、3つのImageButtonを使用してカスタムレイアウトを作成し、それをアラートに設定することです。

builder.setView(customLayout);
于 2012-07-09T07:25:49.153 に答える
1

このコードを試してください:

            LayoutInflater inflater=LayoutInflater.from(YourActivityName.this);
            View view=inflater.inflate(R.layout.buttton, null);
            AlertDialog.Builder builder=new AlertDialog.Builder(YourActivityName.this);
            builder.setView(view);
            builder.setTitle("Are you sure you want to exit?");
            Button posButton=(Button) view.findViewById(R.id.pos);
            Button neuButton=(Button) view.findViewById(R.id.neu);
            Button negButton=(Button) view.findViewById(R.id.neg);
            builder.create();
            builder.show();

buttonxmlファイルを次のように膨らませます。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button 
        android:id="@+id/pos"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
     <Button 
        android:id="@+id/neu"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>
      <Button 
        android:id="@+id/neg"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"/>


</LinearLayout>

そして最後に、クリックイベントを個人に与えることができます。これがお役に立てば幸いです。

于 2012-07-09T07:35:14.467 に答える
0
   CustomDialog dialog = new Dialog(MyActivity.this);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                                        dialog.setContentView(R.layout.login);

                    final EditText editTextEmailAddress = (EditText) dialog
                            .findViewById(R.id.editTextEmailAddress);
                    final EditText editTextPassword = (EditText) dialog
                            .findViewById(R.id.editTextPassword);

                    TextView txtViewForgetPswd = (TextView) dialog
                            .findViewById(R.id.txtViewForgetPswd);

                    txtViewForgetPswd.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            // RETRIVE PASSWORD
                            dialog.dismiss();
                        }
                    });

                    ImageButton imgBtnSubmit = (ImageButton) dialog
                            .findViewById(R.id.imgBtnSubmit);
                    imgBtnSubmit.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // CALL WEBSERVICE OR ASYNTASK TO LOG IN USER
                            String userName = editTextEmailAddress.getText()
                                    .toString();
                            String password = editTextPassword.getText().toString();

                            if (userName.equals("") && password.equals("")) {
                                Toast.makeText(BibleActivity.this,
                                        "Username or password cannot be empty.",
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                                            }

                        }
                    });

                    ImageButton imgBtnCancel = (ImageButton) dialog
                            .findViewById(R.id.imgBtnCancel);
                    imgBtnCancel.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // CLOSE DIALOG HERE FROM CROSS BUTTON
                            dialog.dismiss();
                        }
                    });

                    ImageButton btnCancelCross = (ImageButton) dialog
                            .findViewById(R.id.btnCancelCross);
                    btnCancelCross.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // CLOSE DIALOG HERE FROM CROSS BUTTON
                            dialog.dismiss();
                        }
                    });
                    dialog.show();

//// XML LAYOUT "login" ///

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="480dp"
    android:background="@drawable/mbc_login" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true" >

        <ImageButton
            android:id="@+id/btnCancelCross"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:layout_marginRight="20dp"
            android:background="@drawable/cross" >
        </ImageButton>
    </FrameLayout>

    <TextView
        android:id="@+id/txtViewEmailLbl"
        android:layout_width="200dp"
        android:layout_height="20dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Email address"
        android:textColor="#000000" >
    </TextView>

    <EditText
        android:id="@+id/editTextEmailAddress"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_below="@+id/txtViewEmailLbl"
        android:layout_centerHorizontal="true"
        android:hint="example@eg.com"
        android:imeOptions="actionNext" >
    </EditText>

    <TextView
        android:id="@+id/txtViewPswdLbl"
        android:layout_width="200dp"
        android:layout_height="20dp"
        android:layout_below="@+id/editTextEmailAddress"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Password"
        android:textColor="#000000" >
    </TextView>

    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_below="@+id/txtViewPswdLbl"
        android:layout_centerHorizontal="true"
        android:hint="password123"
        android:imeOptions="actionNext"
        android:inputType="textPassword" >
    </EditText>

    <TableRow
        android:id="@+id/tblRowSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPassword"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp" >

        <ImageButton
            android:id="@+id/imgBtnSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@drawable/btn_submit" >
        </ImageButton>

        <ImageButton
            android:id="@+id/imgBtnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_cancel" >
        </ImageButton>
    </TableRow>

    <TextView
        android:id="@+id/txtViewForgetPswd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tblRowSubmit"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:text=" Forget Password ? "
        android:textColor="#306EFF"
        android:textStyle="italic" >
    </TextView>
</RelativeLayout>

于 2012-07-09T07:41:46.453 に答える