Android:一个活动上的多个对话框

时间:2015-01-21 12:40:17

标签: android android-dialog

我在TextView中有两个Activity(但会添加更多)。

当用户按下TextView时,会出现包含一些文字的Dialog。但是,我希望文本内容在Dialog中更改,具体取决于按下TextView

onPrepareDialog()作为解决方案出现了很多但现在已弃用了) 该应用程序是一个信息应用程序在不同的文本视图上有一个“单击此处”选项,按下时,每个文本视图将显示一个包含不同文本内容和不同标题的对话框。只是想知道最好的方法是什么。谢谢!

到目前为止

代码......

 public class HomeDialogBox extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Message here - to change depending on textview pressed")
            .setPositiveButton("OK Button", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
                        //This method will be invoked when a button in the dialog is clicked.
                }
            );

    builder.setTitle("Title to change depending on textview pressed ");
    // Create the AlertDialog object and return it
    return builder.create();
}

2 个答案:

答案 0 :(得分:0)

是不是可以为每个按钮创建两个单独的Dialog实例?

只需要一个创建Dialog的方法,并根据按下的按钮使用builder.setMessage

创建包含预期消息的对话框

答案 1 :(得分:0)

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.oms.track.FinishedSingleItemView" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp"
    android:text="Large Text one"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="114dp"
    android:text="Large Text two"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/textView1"
    android:layout_marginBottom="90dp"
    android:text="Large Text three"
    android:textAppearance="?android:attr/textAppearanceLarge" />

MainActivity.java

TextView t1,t2,t3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    t1 = (TextView) findViewById(R.id.textView2);
    t2 = (TextView) findViewById(R.id.textView1);
    t3 = (TextView) findViewById(R.id.textView3);

    t1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            showAlertDialog1(MainActivity.this, "text view1","touched text view 1", true);
        }
    });
    t2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            showAlertDialog1(MainActivity.this, "text view2","touched text view 2", true);
        }
    });
    t3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    showAlertDialog1(MainActivity.this, "text view3","touched text view 3", true);
    }
    });
}         

public void showAlertDialog1(Context context, String title, String message, Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    alertDialog.setCancelable(true);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    alertDialog.show();
}

}