您的位置:首页 > 移动开发 > Android开发

Android开发记录三之单选、多选按钮及进度条

2014-07-07 23:11 393 查看
1.单选按钮

布局文件设置:

<?xml version="1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/rb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/male"

/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/female"

/>
</RadioGroup>
</LinearLayout>
java代码实现如下:
rg = (RadioGroup) this.findViewById(R.id.rg);
rb1 = (RadioButton) this.findViewById(R.id.rb1);
rb2 = (RadioButton) this.findViewById(R.id.rb2);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (rb1.getId() == checkedId) {
Toast.makeText(MainActivity.this, "male",
Toast.LENGTH_SHORT).show();
} else if (rb2.getId() == checkedId) {
Toast.makeText(MainActivity.this, "female",
Toast.LENGTH_SHORT).show();
}
}
});
2.多选按钮

布局文件如下:

<CheckBox
android:id="@+id/a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/aa"
/>
<CheckBox
android:id="@+id/b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bb"
/>
<CheckBox
android:id="@+id/c"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cc"
/>

cb1 = (CheckBox) this.findViewById(R.id.a);
cb2 = (CheckBox) this.findViewById(R.id.b);
cb3 = (CheckBox) this.findViewById(R.id.c);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(MainActivity.this, R.string.aa,
Toast.LENGTH_SHORT).show();
}
}
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(MainActivity.this, R.string.bb,
Toast.LENGTH_SHORT).show();
}
}
});
cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
<pre name="code" class="java"><span style="white-space:pre">					</span>Toast.makeText(MainActivity.this, R.string.cc,Toast.LENGTH_SHORT).show();



3.Toast的使用
Toast.makeText(MainActivity.this, R.string.cc,Toast.LENGTH_SHORT).show(); //参数分别为类名,字符串值,常量值
4.ProgressBar

visibility是可视性,gone是用程序启动才开始,visible是可视,invisible是不可视

<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
/>
<Button
android:id="@+id/bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>


java代码用法:

<span style="white-space:pre">		</span>pb=(ProgressBar)this.findViewById(R.id.pb);
bt=(Button)this.findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {//匿名内部类
// TODO Auto-generated method stub
if(i==0){
pb.setVisibility(View.VISIBLE);
//设置为可见状态
}else if (i<100){
pb.setProgress(i);
pb.setSecondaryProgress(i+10);//设置第二进度条

}else{
pb.setProgress(100);
}
i+=10;//i为全局变量
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息