Android按钮Onclick

use*_*259 40 java android

好的我是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
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的按钮代码

<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" />
Run Code Online (Sandbox Code Playgroud)

ali*_*786 88

如果你在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>
Run Code Online (Sandbox Code Playgroud)

在代码部分:

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();
    } 
}
Run Code Online (Sandbox Code Playgroud)

并且不需要这一切:

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

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

    }
 });
Run Code Online (Sandbox Code Playgroud)

检查一次;


ישו*_*ותך 10

你需要做出同样的方法名在布局XML和Java代码.

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

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

}
Run Code Online (Sandbox Code Playgroud)

建议:
不要android:onClick在XML中使用标记将布局与代码混合.而是使用以下OnClickListener方法将click方法移动到您的类:

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

为布局制作布局,不再需要.当您需要重构支持多个屏幕时,它将节省您宝贵的时间.


小智 6

方法1:

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

方法2:

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();

    }
 });
Run Code Online (Sandbox Code Playgroud)


Erw*_*ald 1

使用这样的东西:

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