如何在Alertdialog中显示textview

时间:2014-04-24 06:15:00

标签: android alertdialog android-alertdialog dialog

地狱我正在尝试进行警报对话以显示高分。但我不能在我的对话中添加textview,在textview中添加高分商店。这是我的代码,当我点击NO按钮时,我需要显示textview。

   AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                    .setMessage("Time is up!")
                    .setCancelable(false)
                    .setPositiveButton("Restart!",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, close
                            // current activity
                            App2Activity.this.finish();
                        }
                      })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();

                        }
                    });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();






        }
     }.start();             

1 个答案:

答案 0 :(得分:2)

创建xml文件dialog_layout_pro.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
 android:layout_height="fill_parent"
    android:background="#dedede"
    >
  <TextView
android:id="@+id/tv_dialog"
android:layout_margin="30dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#444444"
android:text="Please wait..."
android:textSize="30sp" />
</RelativeLayout>

和java类

{

 Dialog cusDialog;
 cusDialog = new Dialog(MyTestClass.this, R.style.CustomDialog);
 cusDialog.setContentView(R.layout.dialog_layout_pro);
   TextView text = (TextView) cusDialog.findViewById(R.id.tv_dialog);
    text.setText("here your counter value");

 cusDialog.setCancelable(false);
 cusDialog.show();
 }