开发者

Android: impelment onclick() for 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开发者_C百科: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


This happening because you have the following in your 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" />

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:

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


Why are you trying to make the code more complex. Just try to do this :

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


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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜