Android Studio click event

Android Studio click event

Note: If you are copying all the code, please pay attention to change the package name! ! !

Listener binding

XML file code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:onClick="login"
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认" />

        <Button
            android:onClick="cancel"
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>

Background code:

package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}
    //点击事件
    //login和cancel为xml文件中控件绑定的函数名
    public void login(View v){
        Toast.makeText(MainActivity.this,"监听器绑定事件",Toast.LENGTH_LONG).show();
    }
    public void cancel(View v){
        Toast.makeText(MainActivity.this,"已退出",Toast.LENGTH_LONG).show();
    }
}

Inner class

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button

            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认" />

        <Button

            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>

Background code:

package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    
	//定义按钮的全局变量
    private Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 绑定按钮的资源(利用id找到xml文件中的按钮)
        button1 = (Button)findViewById(R.id.button);
        button2 = (Button)findViewById(R.id.button2);
        
        //设置按钮的监听事件,new一个自己定义的事件名称
        button1.setOnClickListener(new login());
        button2.setOnClickListener(new cancel());

    }
    
    //点击事件
    //实现OnClickListener接口
	//login,cancel为上面设置的监听事件自己定义的名称,来继承OnClickListener接口
    private class login implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"你点击了确认",Toast.LENGTH_LONG).show();
        }
    }

    private class cancel implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"你点击了取消",Toast.LENGTH_LONG).show();
        }
    }

}

Inner anonymous class

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button

            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认" />

        <Button

            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>

Background code:

package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
	//定义按钮的全局变量
    private Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //绑定按钮的资源(利用id找到xml文件中的按钮)
        button1 = (Button)findViewById(R.id.button);
        button2 = (Button)findViewById(R.id.button2);

		//点击事件
		//设置按钮的监听事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"登陆成功!",Toast.LENGTH_SHORT).show();
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
            }
        });
    }

}

Interface implementation

Press and hold the shortcut key Alt+Enter to add packages and methods

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button

            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认" />

        <Button

            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>

Background code:

package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定按钮的资源(利用id找到xml文件中的按钮)
        button1 = (Button)findViewById(R.id.button);
        button2 = (Button)findViewById(R.id.button2);

        //绑定事件监听
        button1.setOnClickListener(this);
        //this指的是当前窗体的事件
        button2.setOnClickListener(this);

    }

//    点击事件,实现事件方法
//    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                Toast.makeText(MainActivity.this,"登陆成功!",Toast.LENGTH_LONG).show();
                break;
            case R.id.button2:
                Toast.makeText(MainActivity.this,"退出!!",Toast.LENGTH_LONG).show();
                break;
        }

    }

}

Toast

makeText method: Generate standard Toast
Toast.makeText(Context context,CharSequence text,int duration);
context:代表上下文,(this或者类的名称.this)
text:要显示的文本
duration:持续的时间,可取值:Toast.LENGTH_LONG,Toast.LENGTH_SHORT

Toast.LENGTH_LONG:较长的视图持续时间
Toast.LENGTH_SHORT:较短的视图持续时间

Toast.makeText(MainActivity.this,"第三种点击事件的实现",Toast.LENGTH_LONG).show();

Guess you like

Origin blog.csdn.net/leilei__66/article/details/108979821