Button和ImageButton[通俗易懂]

Button和ImageButton

大家好,又见面了,我是全栈君。

Button—-button

ImageButton—-图片button 

共同拥有特征:

都能够作为一个button产生点击事件

不同点

1、 Button有text的属性。ImageButton没有

2、 ImageButton有src属性。Button没有

onClick事件

Button和ImageButton都有一个onClick事件

通过他们自身的.setOnClickListener(OnclickListener)方法加入点击事件。

事实上全部的控件都有一个onClick事件

监听事件实现的几种写法

1、 匿名内部类的实现

2、 独立类的实现

3、 实现接口的方式实现 

findViewById—-返回的是一个View对象,须要对其进行类型转换,转换成对应的控件类型。

 以下看一下三种方法的详细实现

package com.example.button;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private TextView tv;
	private Button loginButton;
	private ImageButton imgBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        /*
         * 1、初始化当前须要的控件
         * 2、设置Button的监听器。通过监听器实现我们点击Button要操作的事情
         */
        loginButton = (Button) findViewById(R.id.button1);
        /*
         * 1、监听事件通过匿名内部类实现
         */
        loginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//在当前onClick方法中监听点击Button的动作
				tv.setText("被点击了!

"); } }); /* * 2、监听事件通过独立类实现 */ loginButton = (Button) findViewById(R.id.button2); loginButton.setOnClickListener(listener); /* * 3、通过接口方式实现 */ imgBt = (ImageButton) findViewById(R.id.imageButton1); imgBt.setOnClickListener(this); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View arg0) { tv = (TextView) findViewById(R.id.textView1); //tv.setText("独立类的监听事件被触发了!"); Log.i("tag", "独立类"); } }; @Override public void onClick(View arg0) { Log.i("tag", "通过接口实现!

"); }}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115760.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • mse pytorch_pytorch scatter

    mse pytorch_pytorch scatter基本概念均方误差(meansquareerror,MSE),是反应估计量与被估计量之间差异程度的一种度量,设ttt是根据子样确定的总体参数θ\thetaθ的一个估计量,(θ−t)2(\theta-t)^{2}(θ−t)2的数学期望,称为估计量ttt的均方误差。pytorch中MSELoss函数介绍torch.nn.MSELoss(size_average=True,reduce=Tru…

    2022年9月18日
  • c3p0连接池配置模板,SSM中使用c3p0连接池配置属性

    c3p0连接池配置模板,SSM中使用c3p0连接池配置属性文章目录SSM使用c3p0连接池配置c3p0.properties属性文件Spring核心配置文件中配置applicationContext.xml2.c3p0-config.xmlSSM使用c3p0连接池配置c3p0.properties属性文件#c3p0连接池属性文件#四大基本信息c3p0.driverClass=com.mysql.jdbc.Driverc3p0.jdbcUrl=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&am

    2022年6月11日
  • qt 5.12.1 下载安装详细教程

    qt 5.12.1 下载安装详细教程前言:Qt是一个跨平台的C++图形界面应用程序框架。它提供给开发者建立图形用户界面所需的功能,广泛用于开发GUI程序,也可用于开发非GUI程序。Qt很容易扩展,并且允许真正地组件编程。基本上,Qt同XWindow上的Motif,Openwin,GTK等图形界面库以及Windows平台上的MFC、OWL、VCL、ATL是同类型的东西。一:下载地址http://download.qt.io/of…

    2022年5月13日
  • windows服务器解决挖矿程序问题

    windows服务器解决挖矿程序问题windows服务器解决挖矿程序问题

    2022年6月17日
  • XLSTransformer+模板 导出 Excel「建议收藏」

    XLSTransformer+模板 导出 Excel「建议收藏」java快速导出Excel 利用XLSTransformer+模板

    2022年7月24日
  • myBatis源码学习之SqlSessionFactory

    myBatis源码学习之SqlSessionFactory/***Createsan{@linkSqlSesion}outofaconnectionoraDataSource**@authorClintonBegin*///SqlSessionFactory接口,通过openSession方法获得SQLSessionpublicinterfaceSqlSessionFactory{SqlS

    2022年5月11日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号