Java的代码

主要重点:

  • findViewById();
  • OnClickListener()
 package com.example.admin.myapplication;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t;
Button but1;
Button but2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t =findViewById(R.id.t1);
but1=findViewById(R.id.b1);
but2 =findViewById(R.id.b2);
//通过findViewById()初始化控件 //点击事件方法1
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
t.setText("点击了"+ but2.getText().toString());
}
});
}
//点击事件方法2
public void b(View view){
t.setText("点击了"+ but1.getText().toString());
}
}

xml文件主要是设计手机界面(UI)

重点:控件的运用 (Button TextView)以及属性的设置

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/t1"
android:text="input"
android:textSize="25sp"
android:gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="按钮1"
android:onClick="b"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="按钮2"
/>
</LinearLayout>

app界面如图

Button控件的点击事件的更多相关文章

  1. Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)

    使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();

  2. android中RecyclerView控件实现点击事件

    RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...

  3. 【MAUI】为 Label、Image 等控件添加点击事件

    一.前言 已经习惯了 WPF.WinForm 中"万物皆可点击"的方式. 但是在 MAUI 中却不行了. 在 MAUI 中,点击.双击的效果,是需要通过"手势识别器&qu ...

  4. Android监听Button和ImageButton控件的点击事件

    一.onClick事件 Button和ImageButton都有一个onClick事件,通过自身的.setOnClickListener(OnClickListener)方法添加点击事件 所有的控件都 ...

  5. Android简单的ListViewDemo及每个控件的点击事件

    ListView是什么? ListView是一个 数据控件,可以展示从数据库中读取的数据.是.net3.5的新控件. 它比gridview更灵活,而且支持多种模板,支持分页. 文章地址 http:// ...

  6. Android技能杂谈——如何优雅的处理控件的点击事件

    首先分享一个最佳实践: 大家开发复杂一些的Android应用的时候常常会写一个BaseActivity或者BaseFragment,里面写上一些基本而公用的方法,比如监听网络状况.弹出对话框.设置ac ...

  7. 继承UIView的子控件添加点击事件

    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@select ...

  8. ios 给移动的控件添加点击事件

    前言: 给一个UIView做移动动画,虽然看起来frame在持续改变,但是它的frame已经是最终值了. 也就是说表面看到的动画都是假象,它的真实位置已经是固定的了.所以只有点击在他的真实frame范 ...

  9. listview适配器中的控件的点击事件并传值

    @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto ...

随机推荐

  1. .net(C#)在Access数据库中执行sql脚本

    自己写的一个工具类,主要是业务场景的需要. 主要有两个功能: ①执行包含sql语句的字符串 ②执行包含sql语句的文件 调用方式 /// <summary> /// 执行sql语句 /// ...

  2. PAT1115:Counting Nodes in a BST

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. sniffer pro 使用方法

    一.捕获数据包前的准备工作 在默认情况下,sniffer将捕获其接入碰撞域中流经的所有数据包,但在某些场景下,有些数据包可能不是我们所需要的,为了快速定位网络问题所在,有必要对所要捕获的数据包作过滤. ...

  4. Spring Security 实战:QQ登录实现

    准备工作 1.在 QQ互联 申请成为开发者,并创建应用,得到APP ID 和 APP Key.2.了解QQ登录时的 网站应用接入流程.(必须看完看懂) 为了方便各位测试,直接把我自己申请的贡献出来:A ...

  5. HoloLens开发手记-实现3D应用启动器

    一直有人问我,第三方应用在HoloLens上可以实现3D启动图标吗?就像微软官方出的应用那样.在不久之前,这个问题的答案还是No. 但是随着最新的Windows build 1803版本的发布,现在我 ...

  6. 集合-Table

    当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map<FirstName, Map<LastName, Person>>来实现.为此Guava提供了一个新 ...

  7. bzoj 1592 dp

    就是dp啊 f[i][j]表示到第i位,最后一位高度是j的最小花费 转移::f[i][j]=minn(f[i-1][k])+abs(a[i]-num[j]);(k<=j) #include< ...

  8. BZOJ_1040_[ZJOI2008]骑士_树形DP

    BZOJ_1040_[ZJOI2008]骑士_树形DP 题意: Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪 ...

  9. LCA 各种神奇的LCA优化方法

    LCA(Least Common Ancestors) 树上问题的一种. 朴素lca很简单啦,我就不多说了,时间复杂度n^2 1.倍增LCA 时间复杂度 nlongn+klogn 其实是一种基于朴素l ...

  10. 查看Linux下系统资源占用常用命令(top、free、uptime)

    本文介绍下,在linux中查看系统资源占用的三个命令:top.free.uptime,通过实例学习下它们的用法,有需要的朋友参考下 一,top命令 1.作用top命令用来显示执行中的程序进程,使用权限 ...