java - Android 中来自 XML 的对话框的按钮单击事件

标签 java android android-layout android-activity android-dialog

我正在开发一个在对话框中有很多按钮(如键盘)的应用程序。我需要获取点击按钮的文本并分配给变量。我在这里创建了一个通用功能(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>

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

弹出窗口

<?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 中为对话框或任何其他解决方案中的按钮引发事件。

最佳答案

弹出窗口.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>

   

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

关于java - Android 中来自 XML 的对话框的按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28186011/

相关文章:

java - org.hamcrest.Matchers 用于同时匹配对象的不同属性

android - 如何以编程方式管理线性子布局?

android - 不同 fragment 的多个工具栏布局

java - Log4j2 和 Maven - 选择错误的配置文件

java - 什么时候克隆对象可能不是原始对象的类型?

android - kotlin 中内联函数的正确用法是什么?

android - 将字符串转换为 uri 为位图以显示在 ImageView 中

android - 当页面滚动时滚动 ViewPager 的 RecyclerView

android - 相对布局中的循环依赖错误。

java - Try-with-resources 关闭生成的 child 的套接字