android 中给按钮注册事件
来源:互联网 发布:linux运维之道 mobi 编辑:程序博客网 时间:2024/05/16 16:02
1.每个需要注册事件的组件编一个id
2.在Main.java文件中用组件名定义一些
3.用findViewById 方法初始化那些变量
4.看源代码
test.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="20dp" android:textColor="@color/colorPrimaryDark"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入2-10个字符" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_weight="1" android:id="@+id/etName"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="20dp" android:textColor="@color/colorPrimaryDark"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入6-10个字符" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_weight="1" android:id="@+id/etPwd"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择性别:" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:textSize="20dp" android:textColor="@color/colorPrimary" /> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rg"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:textColor="@color/colorAccent" android:textSize="10dp" android:text="男" android:id="@+id/rbMale"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:textColor="@color/colorAccent" android:textSize="10dp" android:text="女" android:id="@+id/rbFeMale"/> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="20dp" android:textColor="@color/colorAccent" android:text="兴趣爱好:"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="15dp" android:textColor="@color/colorAccent" android:id="@+id/checkbox1" android:text="唱歌"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="15dp" android:textColor="@color/colorAccent" android:id="@+id/checkbox2" android:text="游泳"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="15dp" android:textColor="@color/colorAccent" android:id="@+id/checkbox3" android:text="看书"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="6dp" android:textSize="15dp" android:text="所在地"/> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:layout_marginLeft="10dp" android:entries="@array/citys" android:id="@+id/Spcity"> </Spinner> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:layout_marginTop="10dp" android:textSize="20dp" android:id="@+id/btnLogin"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="显示信息" android:id="@+id/tvShow"/></LinearLayout>
Main.java
package com.example.hsy.register;import android.support.annotation.IdRes;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Spinner;import android.widget.TextView;public class MainActivity extends AppCompatActivity { EditText etName,etPwd; Button btnLogin; TextView tvShow; RadioGroup rg; RadioButton rbMale,rbFelMale; CheckBox checkbox1,checkbox2,checkbox3; Spinner spcity; String sex=""; String hobby1="",hobby2="",hobby3=""; String result=""; String city=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); init(); rglistener(); btnloginlistener(); box1listener(); box2listener(); box3listener(); splistener(); } private void splistener() { spcity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { city=(String) spcity.getSelectedItem(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private void box3listener() { checkbox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ hobby3="看书"; } else { hobby3=""; } } }); } private void box2listener() { checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ hobby2="游泳"; } else { hobby2=""; } } }); } private void box1listener() { checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ hobby1="唱歌"; } else { hobby1=""; } } }); } private void btnloginlistener() { btnLogin.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ result="用户名是:"+etName.getText().toString()+"\n"+"密码是:"+ etPwd.getText().toString()+"\n"+"性别是:"+sex+"\n"+"爱好是:"+hobby1+" " +hobby2+" "+hobby3+" "+ "\n"+"所在城市:"+city; tvShow.setText(result); } }); } private void rglistener() { rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId){ if(checkedId== R.id.rbMale) sex="男生"; else sex="女生"; } }); } private void init() { etName=(EditText) findViewById(R.id.etName); etPwd=(EditText) findViewById(R.id.etPwd); btnLogin=(Button) findViewById(R.id.btnLogin); tvShow=(TextView) findViewById(R.id.tvShow); rg=(RadioGroup) findViewById(R.id.rg); rbMale=(RadioButton) findViewById(R.id.rbMale); rbFelMale=(RadioButton) findViewById(R.id.rbFeMale); checkbox1=(CheckBox) findViewById(R.id.checkbox1); checkbox2=(CheckBox) findViewById(R.id.checkbox2); checkbox3=(CheckBox) findViewById(R.id.checkbox3); spcity=(Spinner) findViewById(R.id.Spcity); }}
效果图
阅读全文
0 0
- android 中给按钮注册事件
- 给按钮注册事件监听器
- MVVM如何给BUTTON按钮注册事件
- Android 给按钮绑定事件
- 给分录按钮注册事件,新增分录时设置默认值。
- android中给按钮注册监听器的一种新颖的方法
- Android给按钮添加单击事件
- Android 在xml布局配置文件中给Button按钮添加事件
- 给控件注册事件
- Android EditText中通过drawabeRight等添加了清空按钮 如何给按钮添加点击事件?
- 利用闭包实现给按钮注册单击事件和键盘事件
- 给android中按钮美美容
- Ext中一次给一个组件注册多个事件
- Jack's Notes12——全选、全不选、反选(循环遍历给按钮动态注册事件)
- 给Android的音量按钮添加监听事件
- 给Android的音量按钮添加监听事件
- 给Android的音量按钮添加监听事件
- asp.net中如何给按钮添加事件
- Python R 安装 igraph
- 【动态规划】HDU1559最大子矩阵
- ubuntu 命令大全
- JavaRDD中变量是否新生成
- PHP源码分析-PHP的生命周期
- android 中给按钮注册事件
- 机器学习第十三课(DBSCAN,密度最大值聚类,谱聚类)
- [中等] Uva OJ 1608 Non-boring sequences
- ThreadLocal的理解
- 解决Invalid 'log4jConfigLocation' parameter: Log4j config file[F:/*路径*/]not found问题
- LeetCode-17-Letter-Combinations-of-a-Phone-Number 递归+模拟
- 反向树状数组 + 离散化 HDU 5372
- 为什么int型的数组用memset不能清零(memset的使用规范)
- jquery ajax springMVC 解决跨域问题