当前位置: 移动技术网 > 移动技术>移动开发>Android > Android初级教程 - 四种点击事件

Android初级教程 - 四种点击事件

2020年07月09日  | 移动技术网移动技术  | 我要评论

package com.example.button_fore;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

 


        // 第一种方式是创建一个外部类进行实现
        String string = findViewById(R.id.btn1).toString();
        Log.d("tag", "button1=" + string);
        findViewById(R.id.btn1).setOnClickListener(new MyOnClickListener());

 


        // 第二种方式是直接匿名内部类进行实现
        findViewById(R.id.btn2).setOnClickListener(new MyOnClickListener() {
            public void onClick(View v) {
                // 应用全局上下文,仅此一份
                Toast.makeText(getApplicationContext(), "第二种点击方法",
                        Toast.LENGTH_LONG).show();
            }
        });

 


        // 第三种方式让当前类成为OnClickListener的子类,实现OnClickListener接口,重写方法
        findViewById(R.id.btn3).setOnClickListener(this);
    }
    class MyOnClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            Log.d("tag", "View=" + v.toString());
            Toast.makeText(MainActivity.this, "第一种点击方法", Toast.LENGTH_LONG)
                    .show();
        }
    }

 


    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "第三种点击方法", Toast.LENGTH_LONG).show();
    }
    /*
     * 注意点:

      1.与返回值类型无关,如不使用void,需要return(因void系统默认带return)    

       2.修饰权限服必须为public

     * 3.形参必须为View4.方法名必须和OnClick属性值一样
     */
    // 第四种方式,在布局文件中,定义功能,重写方法
    public void click(View v) {
        Toast.makeText(MainActivity.this, "第四种点击方法", Toast.LENGTH_LONG).show();
    }}

Layou内部:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一种点击事件" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二种点击事件" />
    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第三种点击事件" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="第四种点击事件" />
</LinearLayout>
 

本文地址:https://blog.csdn.net/weixin_43876828/article/details/107197943

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网