Android:为按钮实现 onclick()
我有这个 xml-Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:background="@color/white" android:layout_width="fill_parent" android:layout_height="200px">
<TextView
android:layout_x="0dp"
android:layout_y="10dp"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="@color/white"
android:text="Name: " />
<EditText
android:layout_x = "20px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="15sp"
android:id="@+id/et_username" android:textColor="@color/black"
android:layout_width="150px"
android:layout_height="50px" />
<Button
android:layout_x = "200px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="16sp"
android:layout_width="96px"
android:layout_height="50px"
android:background ="@drawable/login"
android:id="@+id/btn_login"
android:textColor="@color/white"
android:text="next"
android:onClick="onLoginClicked" />
</AbsoluteLayout>
</LinearLayout>
java File :
public class ButtonAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
return LayoutInflater.from(mContext).inflate(R.layout.custom_keyboard, null);
}
public void onLoginClicked(View v) {
Button button = (Button) v;
String key = button.getText().toString();
anotherMethod(key, false);
}
...
}
并且我在这里使用适配器:
GridView gridview = new GridView(context);
gridview2.setAdapter(new KeyboardAdapter(1, context));
任何人都可以告诉我,为什么当我单击按钮时会出现以下错误?
java.lang.IllegalStateException: Could not find a method onLoginClicked(View) in the activity class MainActivity for onClick handler on view class android.widget.Button
I have this xml-Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:background="@color/white" android:layout_width="fill_parent" android:layout_height="200px">
<TextView
android:layout_x="0dp"
android:layout_y="10dp"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="@color/white"
android:text="Name: " />
<EditText
android:layout_x = "20px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="15sp"
android:id="@+id/et_username" android:textColor="@color/black"
android:layout_width="150px"
android:layout_height="50px" />
<Button
android:layout_x = "200px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="16sp"
android:layout_width="96px"
android:layout_height="50px"
android:background ="@drawable/login"
android:id="@+id/btn_login"
android:textColor="@color/white"
android:text="next"
android:onClick="onLoginClicked" />
</AbsoluteLayout>
</LinearLayout>
java File :
public class ButtonAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
return LayoutInflater.from(mContext).inflate(R.layout.custom_keyboard, null);
}
public void onLoginClicked(View v) {
Button button = (Button) v;
String key = button.getText().toString();
anotherMethod(key, false);
}
...
}
and I use the adapter here:
GridView gridview = new GridView(context);
gridview2.setAdapter(new KeyboardAdapter(1, context));
can anybody tell me, why do I get the following error when I click the button?
java.lang.IllegalStateException: Could not find a method onLoginClicked(View) in the activity class MainActivity for onClick handler on view class android.widget.Button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发生这种情况是因为您的 xml 中有以下内容:
最后一行意味着单击此按钮时,将调用一个方法。该方法名为“onLoginClicked”,它应该是公共的,并且有一个
View
类型的参数,并在Activity类中定义。因此,转到您的活动并写下类似以下内容:
This happening because you have the following in your xml:
The last line means that when this button is clicked, a method will be invoked. This method is named "onLoginClicked", it should be public and have a parameter of type
View
and be defined in the Activity class.So, go to your activity and write something like:
为什么你要让代码变得更复杂。
尝试这样做:
Why are you trying to make the code more complex.
Just try to do this :
我将从布局 XML 中删除 onClick 参数,并使用侦听器处理点击。将此代码添加到活动中的 onCreate() 方法中:
I would remove the onClick parameter from your layout XML and handle the click with a listener. Add this code to your onCreate() method in your activity: