gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:54:26 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com