【问题标题】:Android Button OnclickAndroid 按钮 Onclick
【发布时间】:2020-03-29 07:19:35
【问题描述】:

好的,我是 android dev 和 Java 的新手,所以我在点击方法时遇到了问题,这是我的代码。我知道我必须接近,提前谢谢。我想要我的按钮做的就是,当它在手机上单击以将布局视图从 main.xml 切换到 xx.xml

package my.project;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ExperiencerlActivity extends Activity {
    /** Called when the activ`enter code here`ity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });
    }
}

这是我的按钮代码

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="56dp"
    android:onClick="setLogin"
    android:text="Login" />

【问题讨论】:

  • 不要混合点击事件!

标签: java android


【解决方案1】:

如果你在 xml 文件的 Button 标签中这样写:android:onClick="setLogin" 那么

这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn"
    android:onClick="onClickBtn" />

</LinearLayout>

在代码部分:

public class StartUpActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);    
    }

    public void onClickBtn(View v)
    {
        Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
    } 
}

而且不需要这一切:

 Button button = (Button) findViewById(R.id.button1);
 button.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
 });

检查一次;

【讨论】:

    【解决方案2】:

    您需要在布局 XML 和 java 代码中使用相同的方法名称。

    如果你使用android:onClick="setLogin",那么你需要创建一个同名的方法,setLogin

    // Please be noted that you need to add the "View v" parameter
    public void setLogin(View v) {
    
    }
    

    建议:
    不要通过在 XML 中使用 android:onClick 标记将布局与代码混合。相反,请使用 OnClickListener 方法将 click 方法移至您的类,例如:

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        // TODO Auto-generated method stub
      }
     });
    

    只为布局做一个布局,仅此而已。当您需要重构 Supporting Multiple Screens 时,它将节省您宝贵的时间。

    【讨论】:

      【解决方案3】:

      方法一:

      public void onClick(View v) {
                Intent i = new Intent(currentActivity.this, SecondActivity.class);
               startActivty(i);
              }
      

      方法二:

      Button button = (Button) findViewById(R.id.mybutton);
       button.setOnClickListener(new OnClickListener() {
      
          public void onClick(View v) {
               Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
      
          }
       });
      

      【讨论】:

        【解决方案4】:

        使用这样的东西:

           public void onClick(View v) {
                    // TODO Auto-generated method stub
                   startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
                }
        

        【讨论】:

          【解决方案5】:

          这里是一些如何添加一个名为 Add 的按钮的示例代码。 您应该将变量声明为成员变量,并且成员变量的命名约定是以字母“m”开头。

          在类上按 Alt+Enter 以添加缺少的引用。

          将此添加到您的 activity_main.xml:

           <Button
                  android:id="@+id/buttonAdd"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="ADD"
               />
          

          将此添加到您的 MainActivity.java:

          public class MainActivity extends AppCompatActivity {
          
              Button mButtonAdd; 
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  mButtonAdd = findViewById(R.id.buttonAdd);
          
                  mButtonAdd.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) {
                          // do something here
                      }
                  });
              }
          }
          

          【讨论】:

            【解决方案6】:

            了解您在按下按钮时尝试执行的代码会很有帮助。您已将 xml 文件中的 onClick 属性设置为名为 setLogin 的方法。为清楚起见,我将删除这一行 android:onClick="setLogin" 并直接从您的 onClick() 方法内部调用该方法。

            您也不能只将显示设置为新的 XML,您需要使用 Intent 启动一个新的活动,这样的方法是合适的

             private void setLogin() {
            
             Intent i = new Intent(currentActivity.this, newActivity.class);
             startActivty(i);
            
             }
            

            然后将新的 Activity 设置为具有新的布局。

            【讨论】:

              【解决方案7】:

              这将为您排序

              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
              
                  Button but1=(Button)findViewById(R.id.button1);
              
                  but1.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
              
                          Intent int1= new Intent(MainActivity.this,xxactivity.class);
                          startActivity(int1);
                      }
                  });
              }
              

              您只需将 xxactivity 修改为您的第二个活动的名称

              【讨论】:

                【解决方案8】:

                对此有两种解决方案:-

                (1)不要在xml中放onClick

                (2)删除

                button.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                            // TODO Auto-generated method stub
                    }
                });
                

                然后放

                public void setLogin(View v) {
                    // Your code here
                }
                

                【讨论】:

                • 请注意,您不能同时使用这两种解决方案!
                【解决方案9】:

                在您的按钮点击中使用 Layout inflater 方法。它会将您当前的 .xml 更改为目标 .xml 文件。 Google for layout inflater 代码。

                【讨论】:

                  猜你喜欢
                  • 2016-09-09
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-04-04
                  • 2014-02-07
                  • 2013-02-28
                  相关资源
                  最近更新 更多