Android开发手记(7) 按钮类控件的使用
来源:互联网 发布:现货黄金分时买卖源码 编辑:程序博客网 时间:2024/06/01 10:34
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"); } }); }}
2、CheckBox状态获取
要获取CheckBox状态,只需要设置OnCheckedChangeListener()即可。
CheckBox chkFootball = (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); } }); }}
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("你的性别是:女"); } } }); }}
0 0
- Android开发手记(7) 按钮类控件的使用
- Android开发手记(8) ProgressDialog的使用
- 按钮控件的使用
- 按钮控件的使用
- 【Android】Android单选按钮控件的使用
- 2.2 按钮类控件的使用
- Android开发笔记(三十七)按钮类控件
- 按钮控件JButton的使用
- Android 开发手记一
- Android开发手记
- Android开发手记
- Android开发之TabHost控件的使用
- Android开发中单选框控件RadioButton的使用
- Android开发--Spinner控件的使用
- android开发中spinner控件的使用
- Android开发---WebView控件的使用
- android开发---7.常用控件的使用
- DatePicker 控件的使用 android 开发
- 字母检测
- poj-1528
- 对数组进行升序和降序排序
- wamp服务器——httpd.exe不能正常工作,0xc000007b应用程序错误
- springMVC配置文件
- Android开发手记(7) 按钮类控件的使用
- char* strlen()学习
- 3.11 三维柱形图
- C++IO流简介
- Core Data浅谈系列之一 : 基础结构
- 获取当前日期与时间
- hashmap、concurrenthashmap、CopyOnWriteArrayList底层实现和区别
- 如何计算的LTE峰值数据率?
- Core Data浅谈系列之二 : 简单的CURD