【问题标题】:How to show textview on Alert dialogue如何在 Alertdialog 中显示 textview
【发布时间】:2014-04-24 06:14:59
【问题描述】:

我正在尝试进行警报对话以显示高分点。但我无法在我的对话中添加 textview,高分存储在 textview 中。这是我的代码,当我单击“否”按钮时,我需要显示 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();             

【问题讨论】:

  • 你的 textView 位于哪里?
  • 最终 TextView 得分 = (TextView)findViewById(R.id.textscore); score.setText(" "+counter);

标签: android android-alertdialog dialog


【解决方案1】:

创建 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();
 }

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 2012-04-29
    • 2020-07-25
    • 2018-08-28
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多