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风格标签控件,此 ...
随机推荐
- Flask学习记录之Flask-Login
Flask-Loging 可以方便的管理用户会话,保护路由只让认证用户访问 http://flask-login.readthedocs.org/en/latest/ 一.初始化Flask-Login ...
- (未解决)android studio:com.android.support:appcompat-v7:22+ Could not found
错误信息如下: Error:Could not +. Searched in the following locations: https://jcenter.bintray.com/com/andr ...
- 用于 Visual Studio 和 ASP.NET 的 Web 应用程序项目部署常见问题
https://msdn.microsoft.com/zh-cn/library/ee942158(v=vs.110).aspx#can_i_exclude_specific_files_or_fol ...
- js中数字计算精度
其实同样的问题在java中也有存在,同样是浮点数的问题 问题这样的: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一 ...
- 能够兼容ViewPager的ScrollView
/** * 能够兼容ViewPager的ScrollView * @Description: 解决了ViewPager在ScrollView中的滑动反弹问题 */ public class Scrol ...
- -_-#【Angular】自定义过滤器
AngularJS学习笔记 <!DOCTYPE html> <html ng-app="Demo"> <head> <meta chars ...
- 【转】Java基础笔记 – 枚举类型的使用介绍和静态导入--不错
原文网址:http://www.itzhai.com/java-based-notes-introduction-and-use-of-an-enumeration-type-static-impor ...
- [Design Pattern] Filter Pattern 简单案例
Filter Pattern,即过滤模式,通过不同的过滤标准,或者低耦合将过滤标准组合在一起,对一组对象进行过滤,属于结构类的设计模式. 下面是一个过滤模式的简单案例. Criteria 定义过滤接口 ...
- 《算法问题实战策略》-chaper14-整数论
Lucas定理: 在组合计数问题中,我们常面临组合数C(n,m)过大而无法直接计算的困境,那么这里的Lucas定理给出了一个较大组合数进行取余运算的一种递归算法. 什么是Lucas定理? Lucas定 ...
- spring配置文件中id与name
1.id属性命名必须满足XML的命名规范,因为id其实是XML中就做了限定的. 2.name属性则没有这些限定,你可以使用几乎任何的名称 3.配置文件中不允许出现两个id相同的<bean> ...