zoukankan      html  css  js  c++  java
  • Android——按钮的事件监听

    关于Button按钮的四种事件监听方法总结

    首先我们在activity_main.xml里面先定义一个Button空间

    <RelativeLayout 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"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />
    
    </RelativeLayout>
    

    好,现在要我们来看点击事件的四种写法:

    1.最常见的一种写法——匿名的内部类做为事件监听器类

    package com.basillee.blogdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private Button btnButton;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnButton=(Button)findViewById(R.id.button1);
            btnButton.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Toast.makeText(MainActivity.this, "button clicked", Toast.LENGTH_LONG).show();
                }
            });
        }
    
    }

    2.内部类做为监听器

    package com.basillee.blogdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	private Button btnButton;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		btnButton=(Button)findViewById(R.id.button1);
    		MyClicked myClicked=new MyClicked();
    		btnButton.setOnClickListener(myClicked);
    	}
    	private class MyClicked implements OnClickListener{
    
    		@Override
    		public void onClick(View arg0) {
    			Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
    		}
    		
    	}
    }
    

    3.Activity直接实现OnClickListener接口

    package com.basillee.blogdemo;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnClickListener{

    private Button btnButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnButton=(Button)findViewById(R.id.button1);
    btnButton.setOnClickListener(this);
    }
    @Override
    public void onClick(View arg0) {
    Toast.makeText(this, "Button clicked!", Toast.LENGTH_LONG).show();
    }

    }

    4.在布局文件中,用android:onClick属性指定对应的方法名实现

    activity_main.xml代码如下:

    <RelativeLayout 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"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" 
            android:onClick="myClick"/>
    
    </RelativeLayout>
    

    MainActivity.java代码如下:

    package com.basillee.blogdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    	}
    	public void myClick(View v){
    		Toast.makeText(getApplicationContext(),"Button clicked!", Toast.LENGTH_LONG).show();
    	}
    }
    

      

     

  • 相关阅读:
    Asp.Net Core 入门(七)—— 安装Bootstrap
    Asp.Net Core 入门(八)—— Taghelper
    Asp.Net Core 入门(五)—— 布局视图_Layout.cshtml
    Asp.Net Core 入门(六)—— 路由
    Asp.Net Core 入门(四)—— Model、View、Controller
    Asp.Net Core 入门(三) —— 自定义中间件
    Asp.Net Core 入门(二)——Startup.cs做了什么
    Asp.Net Core 入门(一)——Program.cs做了什么
    用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具
    【转】java中注解的使用与实例
  • 原文地址:https://www.cnblogs.com/BasilLee/p/4101284.html
Copyright © 2011-2022 走看看