Android开发手记(7) 按钮类控件的使用
1、点击Button改变页面背景色
通过Button改变页面背景色,首先新建相应的对象,让后绑定到Layout上的元素。
final RelativeLayout layout = (RelativeLayout)this.findViewById(R.id.layout);
final Button btnRed = (Button)this.findViewById(R.id.btnRed);
然后向新建的按钮增加单机事件。
btnRed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout.setBackgroundColor(Color.RED);
((Button)view).setText("Is Red");
}
});
完整代码:
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Button");
setContentView(R.layout.activity_main); final RelativeLayout layout = (RelativeLayout)this.findViewById(R.id.layout);
final Button btnRed = (Button)this.findViewById(R.id.btnRed);
final Button btnGreen = (Button)this.findViewById(R.id.btnGreen);
final Button btnBlue = (Button)this.findViewById(R.id.btnBlue); btnRed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnGreen.setText("Green");
btnBlue.setText("Blue");
layout.setBackgroundColor(Color.RED);
((Button)view).setText("Is Red");
}
});
btnGreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnRed.setText("Red");
btnBlue.setText("Blue");
layout.setBackgroundColor(Color.GREEN);
((Button)view).setText("Is Green");
}
});
btnBlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnRed.setText("Red");
btnGreen.setText("Green");
layout.setBackgroundColor(Color.BLUE);
((Button)view).setText("Is Blue");
}
}); }
}
MainActivity.java
2、CheckBox状态获取
要获取CheckBox状态,只需要设置OnCheckedChangeListener()即可。
CheckBox chkBox = (CheckBox) findViewById(R.id.chkFootball);
chkFootball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) strFootball = "Football";
else strFootball = "";
tvResult.setText(strFootball + " " + strBasketball);
}
});
完整代码为:
public class MainActivity extends AppCompatActivity { private String strFootball = "";
private String strBasketball = "";
private TextView tvResult ;
private CheckBox chkFootball;
private CheckBox chkBasketball; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.setTitle("Button");
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.tvResult);
chkFootball = (CheckBox) findViewById(R.id.chkFootball);
chkBasketball = (CheckBox) findViewById((R.id.chkBasketball)); chkFootball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) strFootball = "Football";
else strFootball = "";
tvResult.setText(strFootball + " " + strBasketball);
}
}); chkBasketball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) strBasketball = "Basketball";
else strBasketball = "";
tvResult.setText(strFootball + " " + strBasketball);
}
});
} }
MainActivity.java
3、RadioButton与RadioGroup
要获取RadioGroup内RadioButton的选择状态,为RadioGroup添加选择事件即可。
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
// TODO
}
});
首先在RadioGroup内创建两个RadioButton
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/rGroup"> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:id="@+id/rbMale"
android:checked="false" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:id="@+id/rbFemale"
android:checked="false" /> </RadioGroup>
然后,为RadioGroup设置OnCheckedChangeListener()
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==rbMale.getId()) {
result.setText("你的性别是:男");
}
else if(i==rbFemale.getId()){
result.setText("你的性别是:女");
}
}
});
完整代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView result;
private RadioButton rbMale;
private RadioButton rbFemale;
private RadioGroup rGroup; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.setTitle("Button");
setContentView(R.layout.activity_main); result = (TextView)findViewById(R.id.textView);
rbMale = (RadioButton)findViewById(R.id.rbMale);
rbFemale = (RadioButton)findViewById(R.id.rbFemale);
rGroup = (RadioGroup)findViewById(R.id.rGroup); rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==rbMale.getId()) {
result.setText("你的性别是:男");
}
else if(i==rbFemale.getId()){
result.setText("你的性别是:女");
}
}
}); } }
MainActivity.java
Android开发手记(7) 按钮类控件的使用的更多相关文章
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- 八、pyqt5按钮类控件——QPushButton、QRadioButton、QCheckBox
pyqt5中常用的按钮类控件有QPushButton.QRadioButton.QCheckBox.QToolButton等.这些按钮类的基类都是QAbstracButton类.所以这些类有部分方法是 ...
- [APP] Android 开发笔记 004-Android常用基本控件使用说明
TextView 文本框 EditText控件 Button 与 ImageButton ImageView RadioButton CheckBox复选框 TextView 文本框 ,用于显示文本的 ...
- Android开发学习笔记-自定义组合控件
为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...
- Android开发学习笔记-自定义组合控件的过程
自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...
- android 开发-spinner下拉框控件的实现
Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...
- android开发 自定义图文混排控件
功能:图文混排,可自动缩放字体,如图: 单点触控使用的代码来自:http://blog.csdn.net/xiaanming/article/details/42833893 谢谢博主! 在该dem ...
- visual studio开发工具的C#主流控件属性一览表
visual studio开发工具的C#主流控件属性一览表 详细的介绍了各控制属性的详细中文介绍 C#控件及常用设计整理 1.窗体 1.常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程 ...
- Flutter 标签类控件大全Chip
老孟导读:Flutter内置了多个标签类控件,但本质上它们都是同一个控件,只不过是属性参数不同而已,在学习的过程中可以将其放在放在一起学习,方便记忆. RawChip Material风格标签控件,此 ...
随机推荐
- 30 个 php 操作 redis 常用方法代码例子
这篇文章主要介绍了 30 个 php 操作 redis 常用方法代码例子 , 本文其实不止 30 个方法 , 可以操作 string 类型. list 类型和 set 类型的数据 , 需要的朋友可以参 ...
- [Struts2学习笔记] -- 自定义类型转换
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- java 双重检查模式
java 双重检查模式 在并发环境下 兼顾安全和效率 成例(Idiom)是一种代码层次上的模式,是在比设计模式的层次更具体的层次上的代码技巧.成例往往与编程语言密切相关.双重检查成例(Double C ...
- mysql中lock tables与unlock tables
官网:https://dev.mysql.com/doc/refman/5.0/en/lock-tables.html LOCK TABLES tbl_name [[AS] alias] lock_t ...
- filter高级应用
Filter高级应用: Decorator模式 1)包装类需要和被包装对象 实现相同接口,或者继承相同父类 2)包装类需要持有 被包装对象的引用 在包装类中定义成员变量,通过包装类构造方法,传入 ...
- 【HDOJ】4982 Goffi and Squary Partition
题意就是整数划分,选出和为n的K个整数,其中K-1个数的和为完全平方数S.选择整数时需要从1,2,3..连续选择,当选择整数与n-S相等时,需要跳过n-S,即选择n-S+1.如此选择K-2个数,从而可 ...
- Eucalyptus使用的技术
libvirt Libvirt 库是一种实现 Linux 虚拟化功能的 Linux® API,它支持各种虚拟机监控程序,包括 Xen 和 KVM,以及 QEMU 和用于其他操作系统的一些虚拟产品. N ...
- [置顶] Hibernate运行机理
Hibernate 的缓存体系 一级缓存: Session 有一个内置的缓存,其中存放了被当前工作单元加载的对象. 每个Session 都有自己独立的缓存,且只能被当前工作单元访问. 二级缓存: Se ...
- .NET框架设计—常被忽视的框架设计技巧
阅读目录: 1.开篇介绍 2.元数据缓存池模式(在运行时构造元数据缓存池) 2.1.元数据设计模式(抽象出对数据的描述数据) 2.2.借助Dynamic来改变IOC.AOP动态绑定的问题 2.3.元数 ...
- SqlServer中计算列详解
计算列区别于需要我们手动或者程序给予赋值的列,它的值来源于该表中其它列的计算值.比如,一个表中包含有数量列Number与单价列Price,我们就可以创建计算列金额Amount来表示数量*单价的结果值, ...