Button控件的点击事件
Java的代码
主要重点:
- findViewById();
- OnClickListener()
1 package com.example.admin.myapplication; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 public class MainActivity extends AppCompatActivity { 9 TextView t; 10 Button but1; 11 Button but2; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 t =findViewById(R.id.t1); 17 but1=findViewById(R.id.b1); 18 but2 =findViewById(R.id.b2); 19 //通过findViewById()初始化控件 20 21 //点击事件方法1 22 but2.setOnClickListener(new View.OnClickListener() { 23 @Override 24 public void onClick(View view) { 25 t.setText("点击了"+ but2.getText().toString()); 26 } 27 }); 28 } 29 //点击事件方法2 30 public void b(View view){ 31 t.setText("点击了"+ but1.getText().toString()); 32 } 33 }
xml文件主要是设计手机界面(UI)
重点:控件的运用 (Button TextView)以及属性的设置
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity" 8 android:orientation="vertical" 9 > 10 <TextView 11 android:layout_height="wrap_content" 12 android:layout_width="match_parent" 13 android:id="@+id/t1" 14 android:text="input" 15 android:textSize="25sp" 16 android:gravity="center" 17 /> 18 <Button 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/b1" 22 android:text="按钮1" 23 android:onClick="b" 24 /> 25 <Button 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:id="@+id/b2" 29 android:text="按钮2" 30 /> 31 </LinearLayout>
app界面如图
转载于:https://www.cnblogs.com/xfweb/p/10544315.html
Button控件的点击事件相关推荐
- WPF控件自定义点击事件
WPF部分控件没有点击事件,点击,指的是鼠标按下,然后抬起,形成一次点击,这里写了个类,实现了点击事件如下 class ClickEventAction{static List<ClickEve ...
- 日期控件的点击事件,在js中添加callback属性,不在html中直接添加
总结如下: 1. callback的data是点击事件(事件控件)完成后的回调的日期 2. return true后才能改变值,return false不能改变值 3. \ 是转义,因为在" ...
- Kotlin写界面时诸多控件的点击事件
界面中控件较多的话,每个控件都设置setOnClickListener(this)是很麻烦的,为此抽出了一个Context的扩展类: fun Context.setViewClick(listener ...
- winform代码触发控件的点击事件
有些时候我们需要自动触发窗体某个控件的事件,这时应该怎么做呢,下面一起看一下: 代码实现 比如我们想在窗体的load事件中触发butoon的单击事件 private void Form1_Load(o ...
- Button控件的使用方法
Button控件的使用方法 Button控件的定义 Button控件表示按钮,它继承自TextView控件,既可以显示文本,又可以显示图片,同时也允许用户通过点击来操作,当Button控件被点击时,被 ...
- c#皮肤美化:类似于QQ的Button控件
本C#皮肤美化教程内容是:制作一个类似于QQ的带底纹效果的蓝色按钮Button控件.先看看效果图(分别对应普通.悬停.按下时的状态): 下面就开始正式做.首先让我们新建一个控件库项目,命名为QLFUI ...
- Button控件的基本使用(初识封装、减少代码冗余性、代码优化)
Button是程序用于和用户进行交互的一个重要控件,它的配置属性和TextView差不多,这里我们可以注意观察一下Button众多属性中的两种: 1.属性名android:textAllCaps ...
- Android开发 第2课 控件TextView、Plain Text、ImageView、 Button、ImageButton以及点击事件
控件 TextView 显示文本 Plain Text 输入文本 //将布局xml文件引入到activity当中setContentView(R.layout.activity_main);<! ...
- 设置Button控件创建完毕时触发的事件.
Button控件的creationCompleteEffect样式. 示例: 刷新查看效果. 代码: <?xml version="1.0" encoding="u ...
最新文章
- el table怎么获取最后一行_elementUi表格插入额外一行怎么操作
- Debian Squeeze AMD64安装Oracle 10g x86_64 10.2.0....
- Qt 解决 #error This file requires compiler and library support for the ISO C++ 2011 standard
- python卷子_Python试卷
- 安卓平板 python_使用安卓手机或平板对microbit进行编程
- php 接收传值_php接受post传值的方法
- 网易高并发优化 | 公开课-02
- 聪明贝塔(Smart Beta)
- linux查看行数命令 set,Linux显示文件行数之nl命令详解
- putExtra方法
- 【牛客网SQL篇】SQL必知必会
- 电脑无法连接到网络的解决方法(可能,不妨试一试)
- ESP32/ESP32S2直连腾讯云,实现微信小程序控制
- 用数组统计学生各个分数段成绩的人数
- 考研英一----2006年真题知识点总结
- Carmack(卡马克)传奇的3D引擎开源代码
- RFC1951的部分翻译及原文(1/2) (转)
- 菜鸟读财报,如何从上市公司财报中挖情报?--微博转载
- 为FireFox增加自定义搜索引擎
- 高并发和高可用的常规理解
热门文章
- keil5安装_Keil 5安装教程
- html 属性存储对象,在HTML5 localStorage中存储对象
- jquery上传图片插件(支持各种功能)
- python微信点赞脚本_你写过的最好的 Python 脚本是什么?
- BP神经网络和支持向量机在R语言中的实现
- android开启前台服务_Android 知识点必知之ANR与OOM
- python列表去重函数_对python中两种列表元素去重函数性能的比较方法
- python bp神经网络进行预测_python实现BP神经网络回归预测模型
- solidworks工程图模板与图纸格式的区别_教你DWG如何导入为图纸格式
- 人脸对齐(二十)--PRN