Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在警报对话框中显示文本视图_Android_Textview_Android Alertdialog - Fatal编程技术网

Android 在警报对话框中显示文本视图

Android 在警报对话框中显示文本视图,android,textview,android-alertdialog,Android,Textview,Android Alertdialog,在我的代码中,我有一个警报对话框和一个文本视图。我想在我的AlertDialog中显示这个TextView,但我不知道怎么做。我不知道如何在警报对话框中添加视图 我可以显示我的代码,但我认为它没有用 谢谢 编辑: 谢谢你的回答。我刚做了一个测试,效果非常好 这是我的工作代码: package com.example.testalertdialog; import android.app.Activity; import android.app.AlertDialog; import andro

在我的代码中,我有一个
警报对话框
和一个
文本视图
。我想在我的
AlertDialog
中显示这个
TextView
,但我不知道怎么做。我不知道如何在
警报对话框
中添加
视图

我可以显示我的代码,但我认为它没有用

谢谢

编辑:

谢谢你的回答。我刚做了一个测试,效果非常好

这是我的工作代码:

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);
    }
}

}

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

例如:

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

在这里,您需要设置自己的布局

像这样

final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
它将为您提供
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>

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

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

请尝试此示例代码片段

       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();

在drawable中创建文件名custom\u window\u dialog\u 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>
自定义对话框将如下图所示。享受编码:)


在零件自定义布局中-您可以为alertDialog创建自定义布局,不仅可以在textview中,还可以在任何地方

为了简化:这是我的游戏代码

//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();

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


是的,它很有用。显示您的代码。相关snippetsCode正确且显示对话框。此警报中没有可见的文本视图:(@Yoda for EditText使用最终EditText输入=新建EditText(此);
<?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>
<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>
<?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>
 Dialog cusDialog;
 cusDialog = new Dialog(MyTestClass.this, R.style.CustomDialog);
 cusDialog.setContentView(R.layout.dialog_layout_pro);
 cusDialog.setCancelable(false);
 cusDialog.show();
}
//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();
 ad.setMessage("Chargement");
        ad.show();
        ad.setView(tv1);