Android:按钮的onelment onclick()

| 我有这个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
    
已邀请:
发生这种情况是因为您的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 \”,它应该是公共的,并具有类型为“ 5”的参数,并在Activity类中定义。 因此,进行您的活动并写类似以下内容:
public void onLoginClicked(View v) {
    //toast, log, open activity, etc
}
    
您为什么要使代码更复杂。 只需尝试这样做:
Button b=(Button)findViewId(R.id.btn_login);
b.setOnClickListener(new OnClickListener(){
                //perform your action here            
            });
    
我将从布局XML中删除onClick参数,并使用侦听器处理单击。将此代码添加到您的活动中的onCreate()方法中:
Button button = (Button)findViewById(R.id.btn_login);
            button.setOnClickListener(new OnClickListener(){
                String key = button.getText().toString();
                   anotherMethod(key, false);               
            });
    

要回复问题请先登录注册