天天看點

Android之拖動條(SeekBar和RatingBar)的功能和用法

簡介

      下面通過一個執行個體來學習TabHost,在此對上一篇 

Android之拖動條(SeekBar和RatingBar)的功能和用法

使用的項目進行優化,使用TabHost使界面看起來更加友好。

step1:建立一個項目MyTabHost

step2:設計應用的UI界面    /layout/tabhost.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<!-- 定義第一個标簽頁的内容 -->
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="vertical" android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:id="@+id/tab01">
		<ImageView android:id="@+id/image1" android:layout_width="fill_parent"
			android:layout_height="240px" android:src="@drawable/guitar" />
		<!-- 定義一個拖動條,并改變它的滑動外觀 -->
		<SeekBar android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:max="255"
			android:progress="255" android:id="@+id/seekbar" android:thumb="@drawable/star" />
	</LinearLayout>
	
	
	<!-- 定義第二個标簽頁的内容 -->
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="vertical" android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:id="@+id/tab02">

		<ImageView android:id="@+id/image2" android:layout_width="fill_parent"
			android:layout_height="240px" android:src="@drawable/wall" />
		<!-- 定義一個星級評分條 -->
		<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:max="255"
			android:progress="255" android:numStars="5" android:stepSize="0.5" />
	</LinearLayout>

</TabHost>
           

step3:MyTabHostActivity.java

package cn.roco.tabhost;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TabHost;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MyTabHostActivity extends TabActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TabHost tabHost = getTabHost();
		// 設定使用TabHost布局
		LayoutInflater.from(this).inflate(R.layout.tabhost,
				tabHost.getTabContentView(), true);
		//添加第一個标簽頁
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("SeekBar")
				.setContent(R.id.tab01));
		//添加第二個标簽頁
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("RatingBar")
				.setContent(R.id.tab02));
		
		/**使用SeekBar*/
		final ImageView imageView1=(ImageView) findViewById(R.id.image1);
		SeekBar seekBar=(SeekBar) findViewById(R.id.seekbar);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			//當拖動條的滑塊位置發生改變時觸發該方法
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				//動态改變圖檔的透明度
				imageView1.setAlpha(progress);
			}
		});
		
		/**使用RatingBar*/
		final ImageView imageView2=(ImageView) findViewById(R.id.image2);
		RatingBar ratingBar=(RatingBar) findViewById(R.id.ratingbar);
		ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				//動态改變圖檔的透明度
				imageView2.setAlpha((int)(rating*255/5));
			}
		});
	}
}           

step4:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.roco.tabhost"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="MyTabHostActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>           

step5:部署應用到模拟器上,檢視運作效果

==================================================================================================

  作者:歐陽鵬  歡迎轉載,與人分享是進步的源泉!

  轉載請保留原文位址:

http://blog.csdn.net/ouyang_peng/article/details/9633781 http://blog.csdn.net/ouyang_peng/article/details/9634215 http://blog.csdn.net/ouyang_peng/article/details/9673081 http://blog.csdn.net/ouyang_peng

繼續閱讀