1

ユーザーに質問をするアラートダイアログボックスを作成しようとしています。メッセージ内のテキストの一部は、たとえば赤になります。

これは私が試したことです:

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
                        this);
                String You = "<font color='red'>you</font>?";
                dlgAlert.setMessage("How are " + Html.fromHtml(You));
                dlgAlert.setTitle("Mood");
                dlgAlert.setPositiveButton("Good",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {


                                dialog.dismiss();
                            }
                        });
                dlgAlert.setNeutralButton("Okay",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                dialog.dismiss();
                            }
                        });
                dlgAlert.setNegativeButton("Bad",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                dialog.dismiss();
                            }
                        });
                dlgAlert.setCancelable(false);
                dlgAlert.create().show();

「あなた」という言葉を赤い色に変えているわけではありません。何か案は?

4

2 に答える 2

1

ここにそれを行う1つの方法があります

あなたのActivity

LayoutInflater factory = LayoutInflater.from(this);
View dialog = factory.inflate(R.layout.example_dialog, null);
TextView title = (TextView) dialog.findViewById(R.id.message);
SpannableString text = new SpannableString("Test 123");  
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 1, 2, 0);
text.setSpan(new ForegroundColorSpan(Color.DKGRAY), 2, 3, 0);
text.setSpan(new ForegroundColorSpan(Color.CYAN), 3, 4, 0);

title.setText(text, BufferType.SPANNABLE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialog)
       .setTitle("Title")
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  // Do something
              }
        })
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
              }
        })
       .create()
       .show();

/layouts/example_dialog.xml 内

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

    <TextView
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#F70000" />

</LinearLayout>

を使用して TextColor を設定することもできますHtml.fromHtml

title.setText(Html.fromHtml("<font color='red'>Test</font><font color='blue'>ing</font>"));

タイトルには、タイトルのカスタム ビューを設定することもできます。setCustomTitle(View view)

于 2012-11-24T02:27:02.723 に答える
0

これは、ここに示されているカスタム アラート ボックスなしでは実行できません: http://slayeroffice.com/code/custom_alert/

于 2012-11-24T01:37:02.507 に答える