当前位置: 移动技术网 > IT编程>移动开发>Android > Button控件的点击事件

Button控件的点击事件

2019年03月17日  | 移动技术网IT编程  | 我要评论

黑龙江两车相撞,色色色网,三生三世桃花全集资源

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界面如图

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网