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



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

当用户按下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();
}
难道

不能为每个按钮创建两个单独的对话框实例吗?

只需有一个创建对话框的方法,并根据按下的按钮使用 builder.setMessage 创建包含预期消息的对话框

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" />

主要活动.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();
}

}

最新更新