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

9 篇文章 0 订阅

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为全局变量
			}
		});



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值