按钮单击Android中的XML对话框

问题描述 投票:1回答:2

我正在处理一个在对话框中有很多按钮的应用程序(如键盘)。我需要获取单击按钮的文本并分配给变量。我在这里创建了一个常用函数(showtoast)。但是当我点击弹出窗口中的按钮时,很遗憾停止了。显示没有这样的方法。如果我从java手段上升事件,就没有这样的问题。例:

buttona.setOnClickListener(new OnClickListener() {                                  
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    Button b = (Button)v;
    String text = b.getText().toString();
    Toast.makeText(getApplicationContext(), "button clicked is" + text, Toast.LENGTH_SHORT).show();
}
}

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>

Iactivity.Java

package com.example.sample;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Context c = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showdialog();
    }

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

        dialog.show();
    }
    public void showtoast(View v)
    {
        Button b = (Button)v;
        String text = b.getText().toString();
        Toast.makeText(getApplicationContext(), "button clicked is" + text, Toast.LENGTH_SHORT).show();
    }
}

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>

日志:

01-28 12:05:17.386: E/AndroidRuntime(7875): java.lang.IllegalStateException: Could not find a method showtoast(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button

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

java android android-layout android-activity android-dialog
2个回答
2
投票

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:id="@+id/b"
        android:text="c"
        />
</LinearLayout>

main activity.Java

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

0
投票

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

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.