Android:为按钮实现 onclick()

发布于 2024-10-31 20:16:09 字数 2759 浏览 1 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2024-11-07 20:16:09

发生这种情况是因为您的 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
}

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
}
风渺 2024-11-07 20:16:09

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

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

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            
            });
情丝乱 2024-11-07 20:16:09

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

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

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);               
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文