zoukankan      html  css  js  c++  java
  • Android开发手记(7) 按钮类控件的使用

    1、点击Button改变页面背景色

      通过Button改变页面背景色,首先新建相应的对象,让后绑定到Layout上的元素。

    1  final RelativeLayout layout = (RelativeLayout)this.findViewById(R.id.layout);
    2  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");
                }
            });

      完整代码: 

     1 public class MainActivity extends AppCompatActivity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         this.setTitle("Button");
     7         setContentView(R.layout.activity_main);
     8 
     9         final RelativeLayout layout = (RelativeLayout)this.findViewById(R.id.layout);
    10         final Button btnRed = (Button)this.findViewById(R.id.btnRed);
    11         final Button btnGreen = (Button)this.findViewById(R.id.btnGreen);
    12         final Button btnBlue = (Button)this.findViewById(R.id.btnBlue);
    13 
    14         btnRed.setOnClickListener(new View.OnClickListener() {
    15             @Override
    16             public void onClick(View view) {
    17                 btnGreen.setText("Green");
    18                 btnBlue.setText("Blue");
    19                 layout.setBackgroundColor(Color.RED);
    20                 ((Button)view).setText("Is Red");
    21             }
    22         });
    23         btnGreen.setOnClickListener(new View.OnClickListener() {
    24             @Override
    25             public void onClick(View view) {
    26                 btnRed.setText("Red");
    27                 btnBlue.setText("Blue");
    28                 layout.setBackgroundColor(Color.GREEN);
    29                 ((Button)view).setText("Is Green");
    30             }
    31         });
    32         btnBlue.setOnClickListener(new View.OnClickListener() {
    33             @Override
    34             public void onClick(View view) {
    35                 btnRed.setText("Red");
    36                 btnGreen.setText("Green");
    37                 layout.setBackgroundColor(Color.BLUE);
    38                 ((Button)view).setText("Is Blue");
    39             }
    40         });
    41 
    42     }
    43 }
    MainActivity.java

    2、CheckBox状态获取

      要获取CheckBox状态,只需要设置OnCheckedChangeListener()即可。

    1 CheckBox chkBox = (CheckBox) findViewById(R.id.chkFootball);
    2 chkFootball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    3             @Override
    4             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    5                 if (b) strFootball = "Football";
    6                 else strFootball = "";
    7                 tvResult.setText(strFootball + " " + strBasketball);
    8             }
    9         });

      完整代码为:

     1 public class MainActivity extends AppCompatActivity {
     2 
     3     private String strFootball = "";
     4     private String strBasketball = "";
     5     private TextView tvResult ;
     6     private CheckBox chkFootball;
     7     private CheckBox chkBasketball;
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12 //        this.setTitle("Button");
    13         setContentView(R.layout.activity_main);
    14         tvResult = (TextView) findViewById(R.id.tvResult);
    15         chkFootball = (CheckBox) findViewById(R.id.chkFootball);
    16         chkBasketball = (CheckBox) findViewById((R.id.chkBasketball));
    17 
    18         chkFootball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    19             @Override
    20             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    21                 if (b) strFootball = "Football";
    22                 else strFootball = "";
    23                 tvResult.setText(strFootball + " " + strBasketball);
    24             }
    25         });
    26 
    27         chkBasketball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    28             @Override
    29             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    30                 if (b) strBasketball = "Basketball";
    31                 else strBasketball = "";
    32                 tvResult.setText(strFootball + " " + strBasketball);
    33             }
    34         });
    35     }
    36 
    37 }
    MainActivity.java

      

    3、RadioButton与RadioGroup

      要获取RadioGroup内RadioButton的选择状态,为RadioGroup添加选择事件即可。

    1 rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    2             @Override
    3             public void onCheckedChanged(RadioGroup radioGroup, int i) {
    4                 // TODO
    5             }
    6         });

      首先在RadioGroup内创建两个RadioButton

     1 <RadioGroup
     2         android:layout_width="match_parent"
     3         android:layout_height="match_parent"
     4         android:layout_below="@+id/textView"
     5         android:layout_alignParentLeft="true"
     6         android:layout_alignParentStart="true"
     7         android:id="@+id/rGroup">
     8 
     9         <RadioButton
    10             android:layout_width="wrap_content"
    11             android:layout_height="wrap_content"
    12             android:text="男"
    13             android:id="@+id/rbMale"
    14             android:checked="false" />
    15 
    16         <RadioButton
    17             android:layout_width="wrap_content"
    18             android:layout_height="wrap_content"
    19             android:text="女"
    20             android:id="@+id/rbFemale"
    21             android:checked="false" />
    22 
    23     </RadioGroup>

      然后,为RadioGroup设置OnCheckedChangeListener()

     1  rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     2             @Override
     3             public void onCheckedChanged(RadioGroup radioGroup, int i) {
     4                 if(i==rbMale.getId()) {
     5                     result.setText("你的性别是:男");
     6                 }
     7                 else if(i==rbFemale.getId()){
     8                     result.setText("你的性别是:女");
     9                 }
    10             }
    11         });

      完整代码:

     1 import android.support.v7.app.AppCompatActivity;
     2 import android.os.Bundle;
     3 import android.view.Menu;
     4 import android.view.MenuItem;
     5 import android.widget.CheckBox;
     6 import android.widget.CompoundButton;
     7 import android.widget.RadioButton;
     8 import android.widget.RadioGroup;
     9 import android.widget.TextView;
    10 
    11 public class MainActivity extends AppCompatActivity {
    12 
    13         private TextView result;
    14         private RadioButton rbMale;
    15         private RadioButton rbFemale;
    16         private RadioGroup rGroup;
    17 
    18         @Override
    19         protected void onCreate(Bundle savedInstanceState) {
    20             super.onCreate(savedInstanceState);
    21 //        this.setTitle("Button");
    22             setContentView(R.layout.activity_main);
    23 
    24             result = (TextView)findViewById(R.id.textView);
    25             rbMale = (RadioButton)findViewById(R.id.rbMale);
    26             rbFemale = (RadioButton)findViewById(R.id.rbFemale);
    27             rGroup = (RadioGroup)findViewById(R.id.rGroup);
    28 
    29             rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    30             @Override
    31             public void onCheckedChanged(RadioGroup radioGroup, int i) {
    32                 if(i==rbMale.getId()) {
    33                     result.setText("你的性别是:男");
    34                 }
    35                 else if(i==rbFemale.getId()){
    36                     result.setText("你的性别是:女");
    37                 }
    38             }
    39         });
    40 
    41     }
    42 
    43 }
    MainActivity.java
  • 相关阅读:
    机器学习:简介
    对API的理解
    软件的运行
    大数据:数据库概念及分类
    Python:easygui的安装、导入、使用、设置
    Python:模块详解及import本质
    Python:urllib模块的urlretrieve方法
    jQuery操作checkbox实例
    ASP.NET MVC 路由调试工具Router Debugger
    认识Visual Studio 条件编译
  • 原文地址:https://www.cnblogs.com/doodle777/p/Button_Usage.html
Copyright © 2011-2022 走看看