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. Uncaught SyntaxError: Unexpected end of input

    js报错  原因:输入的意外终止…… 页面代码写的不规范啊……其中的某条语句,没有正常结束…… 或者部分语句“‘’”双引号,单引号没有配对好,被转义了之类的……错误造成的 代码: <script ...

  2. $_GLOBALS超全局数组和global定义的全局变量区别?

    全局变量:主程序中定义的变量(函数外部),只能在主程序中使用,在函数内部不能调用 背景:解决在函数内部调用全局变量的问题 解决方法: 1.在函数内部声名全局变量 <?php public $va ...

  3. php 手机电话正则表达式验证

            function check_telnum1($telnum)        {                             $b1 = (preg_match(" ...

  4. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  5. iOS开发——C篇&数组与指针

    2015-07-17 13:23 编辑 前面我们介绍了关于C语言的内存分配问题,下面我们就开始介绍关于C语言的两个非常重要的知识点:数组与指针 数组与指针其实不仅仅是再C语言中,再OC中(当然OC是内 ...

  6. Python——学习笔记

    list  ['','',''] 类似PHP数组   可以修改 tuple  ('','')  不能修改其中的元素 切片 list[int 开始: int 结束: int 间隔=1] 字符串也可以看成 ...

  7. Scut:GameWebSocketHost 解析

    想使用 Scut 做的是一个短连接项目,所以先直接看 GameWebSocketHost 了. 先来看下 GameWebSocketHost 的成员: protected bool EnableHtt ...

  8. Hazelcase 简介

    原博客地址:http://blog.csdn.net/zhu_tianwei/article/details/47984599 Hazelcast是一种内存数据网格in-memory data gri ...

  9. C++进阶阅读

    推荐的阅读顺序:level 1从<<essential c++>>开始,短小精悍,可以对c++能进一步了解其特性以<<c++ primer>>作字典和课 ...

  10. Android基础知识、四大组件(转)

    Android应用程序使用java语言编写的.Android SDK工具将所有的数据和资源文件以及代码进行编译,打包称为一个apk文件.一个apk文件中的所有代码被认为是一个应用,android系统的 ...