如何在自定义警报对话框中设置文本?

时间:2016-11-28 13:08:55

标签: android alertdialog android-alertdialog

我一直在尝试更改视图中的文本,该文本以编程方式通过自定义警报对话框进行充气。在我的应用中,自定义布局中的文本应根据各种情况进行更改

我尝试使用setText(),但没有任何反应。也没有错误。

这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater inflater = getLayoutInflater();
                View dialogLayout = inflater.inflate(R.layout.dialog, null);
                TextView textView=(TextView)dialogLayout.findViewById(R.id.textView);
                textView.setText("Changed");
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setView(R.layout.dialog);
                textView.setText("Changed");


                builder.setTitle("sdfdsf");
                builder.setPositiveButton("OK",null).create()
                        .show();
        }});
    }
}

dialog.xml

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

    <TextView
        android:text="Course"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textColor="@android:color/holo_red_light"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:text="Room"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView8"
        android:textColor="@android:color/holo_red_light"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView" />

    <TextView
        android:text="Slot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textColor="@android:color/holo_red_light"
        android:layout_below="@+id/textView"
        android:layout_alignStart="@+id/textView8" />

    <TextView
        android:text="1205"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="94dp"
        android:id="@+id/textView4"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView8"
        android:layout_alignBaseline="@+id/textView8"
        android:layout_alignBottom="@+id/textView8"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginStart="22dp" />

    <TextView
        android:text="D"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="81dp"
        android:id="@+id/textView3"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignStart="@+id/textView4" />

    <TextView
        android:text="Oops using C++"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="94dp"
        android:id="@+id/textView6"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView"
        android:layout_alignBaseline="@+id/textView"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignStart="@+id/textView3" />
</RelativeLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.knby.temp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="41dp"
        android:layout_marginBottom="165dp"
        android:id="@+id/button" />
</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

设置

builder.setView(dialogLayout); 

而不是

builder.setView(R.layout.dialog);

你设置两次布局。

答案 1 :(得分:0)

更改此代码

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setView(R.layout.dialog);

到这个

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setView(dialogLayout);

您已添加此行builder.setView(dialogLayout);而不是此builder.setView(R.layout.dialog);

答案 2 :(得分:0)

更改

LayoutInflater inflater = getLayoutInflater(); 

LayoutInflater inflater = LayoutInflater.from(context);

答案 3 :(得分:0)

只需使用您正在使用的内容(Dialog,AlertDialog,Dialog.Builder),如下所示:

Dialog dlog = new Dialog(this , your Theme ) ;
Dlog,setTitle("is this dialog ? ");