簡體   English   中英

在警報對話框中顯示文本視圖

[英]display textview in alert dialog

在我的代碼中,我有一個AlertDialog和一個TextView 我想在我的AlertDialog顯示這個TextView ,但我不知道該怎么做。 我不知道如何在AlertDialog添加View

我可以顯示我的代碼,但我認為它沒有用。

謝謝

編輯:

感謝您的所有回答。 我剛剛做了一個測試,它工作得很好。

這是我的工作代碼:

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

}

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

在自定義布局部分 - 您可以為 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();

一個視圖可以被添加到AlertDialog使用setView方法。

例子:

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

試試這個

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

在這里你需要設置你自己的布局。

像這樣

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>

代碼:-

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

另一個使用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_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 最終結果

簡短回答:您在 show() 之后調用 setView() 這段代碼被忽略/不再處理。 你需要最后調用 show()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM