在警告对话框中显示textview

时间:2013-04-24 12:05:23

标签: android textview alertdialog

在我的代码中,我有一个AlertDialog和一个TextView。我想在TextView中显示此AlertDialog,但我不知道该怎么做。我不知道如何在View中添加AlertDialog

我可以展示我的代码,但我认为它不会有用。

感谢的

编辑:

感谢您的所有答案。我刚做了一个测试,它完美无缺。

这是我的工作代码:

package com.example.testalertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    LinearLayout layout;
    AlertDialog ad;
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new LinearLayout(this);
        ad = new AlertDialog.Builder(this).create();
        tv1 = new TextView(this);
        setContentView(layout);
        tv1.setText("Test");
        ad.setView(tv1);
        ad.show();

    }
}

Edit2:但为什么这段代码不起作用?

package com.example.testalertdialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class MainActivity extends Activity implements OnClickListener{

    LinearLayout layout;
    AlertDialog ad;
    TextView tv1;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new LinearLayout(this);
        tv1 = new TextView(this);
        b1 = new Button(this);
        b1.setOnClickListener(this);
        layout.addView(b1);
        ad = new AlertDialog.Builder(this).create();
        setContentView(layout);
        tv1.setText("Test");
}

    @Override
    public void onClick(View v) {
        if (v == b1) {

        ad.setMessage("Chargement");
        ad.show();
        ad.setView(tv1);
    }
}

}

8 个答案:

答案 0 :(得分:19)

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Create TextView
final TextView input = new TextView (this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
     input.setText("hi");
    // Do something with value!
  }
});

  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
      // Canceled.
  }
});
alert.show();

答案 1 :(得分:3)

在零件自定义布局中,您可以为alertDialog创建自定义布局,不仅可以在textview中创建,还可以创建任何内容。

Link简化:这是我游戏中的代码

//create builder
AlertDialog.Builder builder = new AlertDialog.Builder(GWGPlay.this);
//inflate layout from xml. you must create an xml layout file in res/layout first
LayoutInflater inflater = GWGPlay.this.getLayoutInflater();
View layout = inflater.inflate(R.layout.guesskeyword    /*my layout here*/, null);
builder.setView(layout);
//set 2 main buttons
builder.setPositiveButton("Answer", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    endGame(true);
                }
            });

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

            builder.show();

答案 2 :(得分:0)

可以使用AlertDialog方法将视图添加到setView

示例:

AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(your_textview);
dialog.show();

答案 3 :(得分:0)

试试这个

http://www.mkyong.com/android/android-custom-dialog-example/

这里你需要设置自己的布局。

像这样

final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);

它会在对话框中显示textview

试试这个

// textview的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#B7B7B8" 
        android:text="Hello"/>

</LinearLayout>

代码: -

AlertDialog dlg = new AlertDialog.Builder(context)
    .setView(R.layout.textview)
    .setTitle("Message")
    .setMessage(msg)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //retval = 0;
            Toast.makeText(context,
                msg, Toast.LENGTH_SHORT).show();
        }
    }).create();
dlg.show();

答案 4 :(得分:0)

使用setView方法的另一个例子:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.name_dialog, null));
builder.create().show();

答案 5 :(得分:0)

试试此示例代码段

       Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

答案 6 :(得分:0)

在drawable中创建文件名custom_window_dialog_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle"> <!-- this shape is used as shadow -->
        <padding android:bottom="3dp"
            android:left="3dp"
            android:right="3dp"
            android:top="3dp"/>
        <solid android:color="#44000000"/>
       <!--   <corners android:radius="5dp"/> -->
    </shape>
  </item>
   </layer-list>

然后转到值文件夹打开styles.xml并粘贴下面的代码

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowBackground">@drawable/custom_window_dialog_frame</item>
<item name="android:windowNoTitle">true</item>
</style>

创建要显示的布局。在我的情况下,我已经在文件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>

在MyTestClass.java类中

 Dialog cusDialog;
 cusDialog = new Dialog(MyTestClass.this, R.style.CustomDialog);
 cusDialog.setContentView(R.layout.dialog_layout_pro);
 cusDialog.setCancelable(false);
 cusDialog.show();
}

自定义对话框如下图所示。喜欢编码:)

CustomDialog final result in image

答案 7 :(得分:0)

简短回答:您可以在show()之后调用setView(),此代码将被忽略/不再处理。您需要最后调用show()

 ad.setMessage("Chargement");
        ad.show();
        ad.setView(tv1);