Button和ImageButton[通俗易懂]

Button和ImageButton

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

Button—-button

ImageButton—-图片button 

共同拥有特征:

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

不同点

1、 Buttontext的属性。ImageButton没有

2、 ImageButtonsrc属性。Button没有

onClick事件

ButtonImageButton都有一个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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 201621123075 week06-接口、内部类

    201621123075 week06-接口、内部类

    2022年3月7日
    34
  • Java并发基础:进程和线程之由来

    Java并发基础:进程和线程之由来

    2021年9月13日
    43
  • oracle number字段改为integer「建议收藏」

    oracle number字段改为integer「建议收藏」原文链接:http://www.fengyachao.com/archives/691.原字段没数据alter  table tb_test modifycolinteger;2.原字段有数据altertabletb_test addcol_tempinteger;updatetb_test setcol_temp=col;

    2022年7月24日
    11
  • NTP校时设置

    NTP校时设置一、WindowsServer2008–TimeServer前言:国家时间与频率标准实验室 &&NTP服务器 也可以忽略1~6直接跳7 如果已改过机码请使用 1    Cmd:2     netstopw32time3     w32tm/unregister4     w32tm/register…

    2022年6月24日
    68
  • img图片加载出错处理[通俗易懂]

    img图片加载出错处理[通俗易懂]为了美观当网页图片不存在时不显示叉叉图片当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验。即使使用alt属性给出了”图片XX”的提示信息,也起不了多大作用。其实,可以这样处理:当图片不存在的时候,会触发onerror事件,我们可以在该事件中做一下补救的工作,比如:1、让这个图片元素隐藏:imgsrc=”图片的url地址”

    2022年7月26日
    33
  • .npy文件_文件后缀名是npy怎么打开

    .npy文件_文件后缀名是npy怎么打开深度学习–迁移学习在使用训练好的模型时,其中有一种保存的模型文件格式叫.npy。打开方式·实现代码:importnumpyasnptest=np.load(‘./bvlc_alexnet.npy’,encoding=”latin1″)#加载文件doc=open(‘1.txt’,’a’)#打开一个存储文件,并依次写入print(test,file=doc)#…

    2025年8月21日
    4

发表回复

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

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