ToggleButton、SeekBar、RatingBar使用

个人理解 ToggleButton  主要作用就是切换开关 

本例子展示布局的切换 

 一、ToggleButton  使用

1 布局

   <LinearLayout
        android:id="@+id/layoutId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ToggleButton android:id="@+id/toggleBtn"  
            android:layout_width="wrap_content" 
            			android:layout_height="wrap_content"
            			android:textOn="横排布局"
            			android:textOff="竖排布局"
            			android:checked="true"/>
 

        <Button
            android:id="@+id/main_button_radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:onClick="toDialActivity"
            android:text="拨打电话" />
    </LinearLayout>

 

 2  添加事件监听的方法

  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ToggleButton tb = (ToggleButton) findViewById( R.id.toggleBtn) ;
        tb.setOnCheckedChangeListener( new  toggleCheckChange());
    }


    class toggleCheckChange implements CompoundButton.OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			 LinearLayout layout=  (LinearLayout) findViewById(R.id.layoutId);
			 if(isChecked){
				 layout.setOrientation( LinearLayout.HORIZONTAL);
			 }else{
				 layout.setOrientation( LinearLayout.VERTICAL);
			 }
			
		}
    }

 

二、seekBar使用:拖动进度条 

  

  1 xml中定义 

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="拖动进度条" />
 

 

2  Activity中的方法 

  

		seekBar = (SeekBar) findViewById( R.id.seekBar);
		descTextView= (TextView) findViewById( R.id.description);
		seekBar.setOnSeekBarChangeListener( new seekBarChangeListener());

   //seekBar的监听类。
	class seekBarChangeListener implements SeekBar.OnSeekBarChangeListener{

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			descTextView.setText("进度="+ progress+"%");
		}
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			descTextView.setText(" 开始拖动");
		}
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			descTextView.setText("结束拖动  ");
		}
	}
 

 

三、RatingBar使用 :星级评分使用,可以自定义

   默认三种风格ratingBarStyleIndicator和ratingBarStyleSmall用于显示,不用于交互 

  ratingbar_default 通常用户交互 

 

  1 xml布局 (三种样式不同)

    

        <RatingBar
            android:id="@+id/ratingbar_Indicator"
            style="?android:attr/ratingBarStyleIndicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RatingBar
            android:id="@+id/ratingbar_Small"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="20" />

        <RatingBar
            android:id="@+id/ratingbar_default"
            style="?android:attr/ratingBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

  2 Activity中使用

   

		ratingbar_Indicator = (RatingBar) findViewById(R.id.ratingbar_Indicator) ;
		ratingbar_Small = (RatingBar) findViewById(R.id.ratingbar_Small) ;
		ratingbar_default = (RatingBar) findViewById(R.id.ratingbar_default) ;
		
		ratingbar_default.setOnRatingBarChangeListener(new RatingBarListener());




	/**
	 * 星级评分 
	 * @author root
	 *
	 */
	class  RatingBarListener implements  OnRatingBarChangeListener{
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			Log.e("星级:", rating+"   ");
			Toast.makeText(LayoutActivity.this, "评分:"+rating , Toast.LENGTH_LONG);
		}
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值