【问题标题】:Android: impelment onclick() for buttonAndroid:为按钮执行 onclick()
【发布时间】:2011-04-13 10:49:48
【问题描述】:

我有这个 xml 布局:

 <?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 文件:

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

【问题讨论】:

  • 您的 onLoginClicked(View v) 方法在您的 ButtonAdapter 类而不是 MainActivity 中?
  • 您确定吗,您的 onLoginClicked 方法在 MainActivity 类中,而不是在您的 ButtonAdapter 中 - 它是在 MainActivity 中查找的。​​span>
  • 我有 'ButtonAdapter' 以便在我的 Activity 中不包含所有内容。

标签: android onclick adapter


【解决方案1】:

发生这种情况是因为您的 xml 中有以下内容:

  <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" />

最后一行的意思是当这个按钮被点击时,一个方法会被调用。这个方法被命名为“onLoginClicked”,它应该是公共的,并且有一个View类型的参数并且在Activity类中定义。

所以,去你的活动并写下类似的东西:

public void onLoginClicked(View v) {
    //toast, log, open activity, etc
}

【讨论】:

  • 如何选择查看哪个活动类?我正在尝试实现片段,并且我在那些需要自己的 onclick 功能的片段上有按钮,但我不想将所有内容都塞进一个主要活动中。因此,我已将该片段的 onclick 代码分区到该片段的定义文件中,但 Android 会继续在我的主要活动中搜索我的函数。
【解决方案2】:

你为什么要让代码更复杂。 尝试这样做:

Button b=(Button)findViewId(R.id.btn_login);
b.setOnClickListener(new OnClickListener(){
                //perform your action here            
            });

【讨论】:

  • 我想我做不到,因为我的 Button 不在我的 Activity 类中!
  • 您可以使用 viewHolder() 方法从任何 XML 接收按钮运行时。
  • 感谢您的回答。我应该定义我的视图中的所有按钮吗?
  • 你可以,没问题,只使用你想要添加监听器并执行一些操作的那个。
  • 我已经尝试过了,但是将所有 60 个按钮都放在支架上看起来不太好。我的意思是我有 xml-Layout 为了不以编程方式制作按钮!正如我所说,我的 xml 布局中有多个按钮。这是让它发挥作用的唯一方法吗?
【解决方案3】:

我将从您的布局 XML 中删除 onClick 参数并使用侦听器处理单击。将此代码添加到活动中的 onCreate() 方法中:

Button button = (Button)findViewById(R.id.btn_login);
            button.setOnClickListener(new OnClickListener(){
                String key = button.getText().toString();
                   anotherMethod(key, false);               
            });

【讨论】:

  • 我想我做不到,因为我的 Button 不在我的 Activity 类中!它是 gridlayout 中的一个子元素,它是 viewflipper 的子元素,我有很多这样的按钮。
猜你喜欢
  • 2015-12-24
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多