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) 按钮类控件的使用的更多相关文章

  1. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

  2. 八、pyqt5按钮类控件——QPushButton、QRadioButton、QCheckBox

    pyqt5中常用的按钮类控件有QPushButton.QRadioButton.QCheckBox.QToolButton等.这些按钮类的基类都是QAbstracButton类.所以这些类有部分方法是 ...

  3. [APP] Android 开发笔记 004-Android常用基本控件使用说明

    TextView 文本框 EditText控件 Button 与 ImageButton ImageView RadioButton CheckBox复选框 TextView 文本框 ,用于显示文本的 ...

  4. Android开发学习笔记-自定义组合控件

    为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...

  5. Android开发学习笔记-自定义组合控件的过程

    自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...

  6. android 开发-spinner下拉框控件的实现

    Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...

  7. android开发 自定义图文混排控件

    功能:图文混排,可自动缩放字体,如图: 单点触控使用的代码来自:http://blog.csdn.net/xiaanming/article/details/42833893  谢谢博主! 在该dem ...

  8. visual studio开发工具的C#主流控件属性一览表

    visual studio开发工具的C#主流控件属性一览表 详细的介绍了各控制属性的详细中文介绍 C#控件及常用设计整理 1.窗体 1.常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程 ...

  9. Flutter 标签类控件大全Chip

    老孟导读:Flutter内置了多个标签类控件,但本质上它们都是同一个控件,只不过是属性参数不同而已,在学习的过程中可以将其放在放在一起学习,方便记忆. RawChip Material风格标签控件,此 ...

随机推荐

  1. $_CFG = load_config(); /* 载入系统参数 */

    ecshop 中$_CFG数组主要是放置一些系统参数,并且全站共享的数据,在使用的时候,ecshop里面常常以$GLOBALS['_CFG']全局变量的模式来处理. ecshop 的$GLOBALS[ ...

  2. Codeforces 138D World of Darkraft

    有一个n*m 的棋盘,每个点上标记了L,R,X 中的一个每次能选择一个没有被攻击过的点(i,j),从这个点开始发射线,射线形状为:1. 若字符是 L,向左下角和右上角发,遇到被攻击过的点就停下来2. ...

  3. Tomcat 9.0安装配置

    本文转自:http://blog.sina.com.cn/s/blog_15126e2170102w5o8.html 一.JDK的安装与配置 1.从官网下载jdk,注意是jdk不是jre.最好从官网下 ...

  4. js blog

    http://www.csdn.net/article/2013-12-16/2817820-javascript-survey-results 近日DailyJS社区发起了一项针对JavaScrip ...

  5. h.264参考图像列表、解码图像缓存

    1.参考图像列表(reference picture list) 一般来说,h.264会把需要编码的图像分为三种类型:I.P.B,其中的B.P类型的图像由于采用了帧间编码的这种编码方式,而帧间编码又是 ...

  6. Qt for Android 开发大坑123

    http://blog.csdn.net/qyvlik/article/details/50989685 http://blog.csdn.net/qyvlik/article/details/515 ...

  7. Spring Assert.notNull

    Exception in thread "main" java.lang.IllegalArgumentException: Source must not be null at ...

  8. Linux命令之yes

    yes命令用于重复输出字符串(output a string repeatedly until killed).这个命令可以帮你自动回答命令行提示,例如,进入一个含有多个文件的目录,执行 " ...

  9. UNDO 100%

    另外查了下v$undostat,发现begin_time已经很久没有改变, BEGIN_TIME           END_TIME             MAXQUERYLEN MAXCONCU ...

  10. 等待事件--db file sequential read

    对于最小化db file sequential read 事件所带来的影响,你可以做的另一件事情是减少AVERAGE_WAIT时间. 这是会话必须等待从磁盘提取单块的平均时间,这些信息可以从v$ses ...