如何在Android中创建具有textview的对话框?

时间:2016-12-29 05:51:15

标签: android textview android-alertdialog

我正在尝试在android中创建一个带有var el; var i = 0; var fragment = document.createDocumentFragment(); while (i < 500) { el = document.createElement('li'); el.innerText = '1ist ' + i; fragment.appendChild(el); i++; } div.appendChild(fragment); 的对话框,如:

Image]

为了创建Textview,我使用了以下函数Textview,但没有成功。

6 个答案:

答案 0 :(得分:0)

像这样使用alertdialog,并为对话框创建另一个布局:

final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                @SuppressLint("InflateParams") final View alertlayout = inflater.inflate(R.layout.notif_msg_dialog, null);

                TextView text_main=(TextView)alertlayout.findViewById(R.id.notif_msg);
                text_main.setText("YOUR TEXT");


                builder.setView(alertlayout);
                packsizeDialog=builder.create();
                packsizeDialog.show();

这里,notif_msg_dialog.xml是具有textview

的布局

答案 1 :(得分:0)

            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.dialog);
            TextView text1 = 
                      (TextView)dialog.findViewById(R.id.text1);
            Button proceed =
            (Button)dialog.findViewById(R.id.button);

            proceed.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   dialog.dismiss();

                }
            });

            dialog.show();

dialog.xml

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

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text1"
    android:layout_centerInParent="true"/>

  <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:text="Proceed"
    android:layout_alignParentBottom="true"/>


 </RelativeLayout>

我希望这会有所帮助

答案 2 :(得分:0)

只需使用AlertDialog.Builder并将您的文本放在setMessage中即可显示

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("your text");
        builder.create();
        builder.show();

答案 3 :(得分:0)

您可以通过以下帖子http://stackoverflow.com/questions/10903754/input-text-dialog-android

获得帮助

答案 4 :(得分:0)

使用: -

    private void fn_showAlertDialog() {
        new AlertDialog.Builder(YourActivity.this)
            .setTitle("Title of your dialog")
            .setMessage("Text that you want to show.")
            .setCancelable(false)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //do your task
                    dialog.cancel();
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //do your task
                    dialog.cancel();
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}

答案 5 :(得分:0)

<强> custom_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="80dp"
    android:background="#3E80B4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="Do you realy want to exit ?"
        android:textColor="@android:color/white"
        android:textSize="15dp"
        android:textStyle="bold"/>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#3E80B4"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="Yes"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_no"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="5dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="No"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

您必须扩展Dialog并实现OnClickListener

public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {

  public Activity c;
  public Dialog d;
  public Button yes, no;

  public CustomDialogClass(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    yes = (Button) findViewById(R.id.btn_yes);
    no = (Button) findViewById(R.id.btn_no);
    yes.setOnClickListener(this);
    no.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_yes:
      c.finish();
      break;
    case R.id.btn_no:
      dismiss();
      break;
    default:
      break;
    }
    dismiss();
  }
}

如何调用此方法?

CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();