Java Android中XML对话框的按钮单击事件

Java Android中XML对话框的按钮单击事件,java,android,android-layout,android-activity,android-dialog,Java,Android,Android Layout,Android Activity,Android Dialog,我正在开发一个对话框中有很多按钮的应用程序(比如键盘)。我需要得到点击按钮的文本并分配给变量。我在这里创建了一个公共函数(showtoos)。但当我点击弹出窗口中的按钮时,它不幸停止了。似乎没有这样的方法。如果我把事件从java的意思中提出来,就不存在这样的问题。 例如: activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="

我正在开发一个对话框中有很多按钮的应用程序(比如键盘)。我需要得到点击按钮的文本并分配给变量。我在这里创建了一个公共函数(showtoos)。但当我点击弹出窗口中的按钮时,它不幸停止了。似乎没有这样的方法。如果我把事件从java的意思中提出来,就不存在这样的问题。 例如:

activity_main.xml

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
popup.xml

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


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showtoast"
        android:text="a"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showtoast"
        android:text="b"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showtoast"
        android:text="c"
        />
</LinearLayout>

请任何人帮助从对话框或任何其他解决方案中的按钮的xml引发事件

您不能直接从对话框中调用活动方法,因为您需要实现一个侦听器&一个自定义对话框类

public class CustomDialog extends Dialog {

    public CustomDialog(Context context, String title,
            final ItemReturnListener listener) {
        super(context, R.style.CustomDialog);
        // TODO Auto-generated constructor stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.dialog_custom);

        Button tvDate = (Button) findViewById(R.id.tvDate);
        Button tvName = (Button) findViewById(R.id.tvName);
        Button tvPrice = (Button) findViewById(R.id.tvPrice);

        tvDate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listener.returnString(tvDate.getText().toString());
                dismiss();
            }
        });
        tvName.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listener.returnString(tvName.getText().toString());
                dismiss();
            }
        });
        tvPrice.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listener.returnString(tvPrice.getText().toString());
                dismiss();
            }
        });

        getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }
现在通过调用此方法打开对话框

private void openDialog() {
        dialog = new CustomDialog(MainActivity.this, new ItemReturnListener() {

            @Override
            public void returnString(String str) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "button clicked is" + str, Toast.LENGTH_SHORT).show();
            }
        });

        dialog.show();
    }
创建这样的接口

public interface ItemReturnListener {
    void returnString(String str);
}
popup.xml
使用
MainActivity。此
而不是
getApplicationContext()
来创建
对话框
对象,并添加带有问题的日志以获取更多帮助
public class CustomDialog extends Dialog {

    public CustomDialog(Context context, String title,
            final ItemReturnListener listener) {
        super(context, R.style.CustomDialog);
        // TODO Auto-generated constructor stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.dialog_custom);

        Button tvDate = (Button) findViewById(R.id.tvDate);
        Button tvName = (Button) findViewById(R.id.tvName);
        Button tvPrice = (Button) findViewById(R.id.tvPrice);

        tvDate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listener.returnString(tvDate.getText().toString());
                dismiss();
            }
        });
        tvName.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listener.returnString(tvName.getText().toString());
                dismiss();
            }
        });
        tvPrice.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listener.returnString(tvPrice.getText().toString());
                dismiss();
            }
        });

        getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }
private void openDialog() {
        dialog = new CustomDialog(MainActivity.this, new ItemReturnListener() {

            @Override
            public void returnString(String str) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "button clicked is" + str, Toast.LENGTH_SHORT).show();
            }
        });

        dialog.show();
    }
public interface ItemReturnListener {
    void returnString(String str);
}
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b"
        android:text="c"
        />
</LinearLayout>

   
private void showdialog() {
    // TODO Auto-generated method stub
    Dialog dialog = new Dialog(c);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.popup);
    final Button b = (Button)dialog.getWindow().findViewById(R.id.b);

    b.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"one", 2000).show();
                
            }
        });
    dialog.show();
}