677 本講內(nèi)容:ProgressBar SeekBar RatingBar 點(diǎn)擊下載: 2、各種各樣的圓圈: 注意標(biāo)題欄上也有一個(gè)進(jìn)度條,下面是代碼: - package android.basic.lesson11;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainHelloProgressBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在標(biāo)題條里放置進(jìn)度條。請(qǐng)求窗口特色風(fēng)格,這里設(shè)置成不明確的進(jìn)度風(fēng)格
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
//設(shè)置標(biāo)題欄中的不明確的進(jìn)度條是否可以顯示,當(dāng)你需要表示處理中的時(shí)候設(shè)置為True,處理完畢后設(shè)置為false
setProgressBarIndeterminateVisibility(true);
}
}
復(fù)制代碼 下面試main.xml中的代碼,大家注意黑體字部分的內(nèi)容,看看不同樣式的不同的表現(xiàn):
- <?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”vertical”android:background=”#003399″android:layout_width=”fill_parent”android:layout_height=”fill_parent”><ProgressBar android:id=”@+id/ProgressBar01″android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar><ProgressBar android:id=”@+id/ProgressBar02″style=”?android:attr/progressBarStyleLarge” 大圓圈android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar><ProgressBar android:id=”@+id/ProgressBar03″style=”?android:attr/progressBarStyleSmall” 小圓圈android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar><ProgressBar android:id=”@+id/ProgressBar03″style=”?android:attr/progressBarStyleSmallTitle” 標(biāo)題條的樣式android:layout_width=”wrap_content”android:layout_height=”wrap_content”></ProgressBar>
</LinearLayout>
復(fù)制代碼
3、長(zhǎng)條狀的進(jìn)度條 xml代碼:
- <ProgressBar android:id=”@+id/ProgressBar04″style=”?android:attr/progressBarStyleHorizontal”android:layout_marginLeft=”10dp”
android:layout_marginRight=”10dp”android:max=”100″ 最大刻度按100算android:progress=”30″ 第一進(jìn)度是30android:secondaryProgress=”80″ 第二進(jìn)度是80android:layout_width=”fill_parent” 進(jìn)度條的顯示長(zhǎng)度是鋪滿父窗口android:layout_height=”wrap_content”></ProgressBar><?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”vertical”android:background=”#003399″android:layout_width=”fill_parent”android:layout_height=”fill_parent”><ProgressBar android:id=”@+id/ProgressBar04″style=”?android:attr/progressBarStyleHorizontal”android:layout_marginTop=”10dp”android:layout_marginLeft=”10dp”android:layout_marginRight=”10dp”android:max=”100″android:progress=”30″android:secondaryProgress=”80″android:layout_width=”fill_parent”android:layout_height=”wrap_content”></ProgressBar>
</LinearLayout>
復(fù)制代碼 -
package android.basic.lesson11;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainHelloProgressBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設(shè)置窗口進(jìn)度條特性風(fēng)格
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
//設(shè)置進(jìn)度條可見性
setProgressBarVisibility(true);
//設(shè)置進(jìn)度條進(jìn)度值,要乘以100的
setProgress(60*100);
setSecondaryProgress(80*100);
}
}
復(fù)制代碼
運(yùn)行效果: 同學(xué)們可以使用下面的代碼對(duì),進(jìn)度條進(jìn)行一些操作練習(xí): private ProgressBar pb; //定義ProgressBar pb = (ProgressBar) findViewById(R.id.ProgressBar01);
pb.incrementProgressBy(5); //ProgressBar進(jìn)度值增加5 pb.incrementProgressBy(-5); //ProgressBar進(jìn)度值減少5 pb.incrementSecondaryProgressBy(5); //ProgressBar第二個(gè)進(jìn)度條 進(jìn)度值增加5 pb.incrementSecondaryProgressBy(-5); //ProgressBar第二個(gè)進(jìn)度條 進(jìn)度值減少5
十二、SeekBar 拖動(dòng)條 滑動(dòng)條 SeekBar可以作為音樂(lè)播放器的進(jìn)度指示和調(diào)整工具,音量調(diào)整工具等,SeekBar是ProgressBar的一個(gè)子類,下面我們用一個(gè)可以改變并顯示當(dāng)前進(jìn)度的拖動(dòng)條例子來(lái)演示一下它的使用: 1、main.xml < ?xml version="1.0" encoding="utf-8"?> 2、java: - package android.basic.lesson11;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainHelloSeekBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//找到拖動(dòng)條和文本框
final SeekBar sb = (SeekBar) findViewById(R.id.SeekBar01);
final TextView tv1 = (TextView) findViewById(R.id.TextView01);
//設(shè)置拖動(dòng)條的初始值和文本框的初始值
sb.setMax(100);
sb.setProgress(30);
tv1.setText("當(dāng)前進(jìn)度:" + sb.getProgress());
//設(shè)置拖動(dòng)條改變監(jiān)聽器
OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
tv1.setText("當(dāng)前進(jìn)度:" + sb.getProgress());
Toast.makeText(getApplicationContext(), "onProgressChanged",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "onStartTrackingTouch",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "onStopTrackingTouch",
Toast.LENGTH_SHORT).show();
}
};
//為拖動(dòng)條綁定監(jiān)聽器
sb.setOnSeekBarChangeListener(osbcl);
}
}
復(fù)制代碼 3、運(yùn)行程序: 十三、RatingBar 評(píng)分條 RatingBar評(píng)分條是SeekBar拖動(dòng)條的子類。現(xiàn)階段系統(tǒng)自帶了3種樣式,我們同樣用例子來(lái)演示他的使用方法和屬性設(shè)置: 1、main.xml的代碼:
- <?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”vertical”android:layout_width=”fill_parent”android:layout_height=”fill_parent”><RatingBarandroid:id=”@+id/RatingBar01″android:layout_width=”wrap_content”android:rating=”3″ 默認(rèn)分值設(shè)為3android:layout_marginTop=”20dp”android:layout_height=”wrap_content”></RatingBar>
復(fù)制代碼
2、在HelloRatingBar.java的代碼里我們實(shí)現(xiàn)了3個(gè)評(píng)分條聯(lián)動(dòng)的功能: - package android.basic.lesson11;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
public class MainHelloRatingBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定義組件對(duì)象
final RatingBar rb1 = (RatingBar)findViewById(R.id.RatingBar01);
final RatingBar rb2 = (RatingBar)findViewById(R.id.RatingBar02);
final RatingBar rb3 = (RatingBar)findViewById(R.id.RatingBar03);
//定義評(píng)分監(jiān)聽器
OnRatingBarChangeListener orbcl= new OnRatingBarChangeListener(){
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
switch(ratingBar.getId()){
case R.id.RatingBar01:
//把第一個(gè)評(píng)分條的值取出來(lái)設(shè)置給其他評(píng)分條
rb2.setRating(rb1.getRating());
rb3.setRating(rb1.getRating()*2);//十顆星所以乘以2
break;
case R.id.RatingBar02:
rb1.setRating(rb2.getRating());
rb3.setRating(rb2.getRating()*2);
break;
case R.id.RatingBar03:
rb1.setRating(rb3.getRating()/2);
rb2.setRating(rb3.getRating()/2);
break;
}
}
} ;
//綁定監(jiān)聽器
rb1.setOnRatingBarChangeListener(orbcl);
rb2.setOnRatingBarChangeListener(orbcl);
rb3.setOnRatingBarChangeListener(orbcl);
}
}
復(fù)制代碼 3、運(yùn)行程序,點(diǎn)擊評(píng)分條看看效果,使用左右鍵再試試 6有興趣的同學(xué)可以研究一下,如何更換評(píng)分條中的圖片,譬如罷星星換成花朵什么的。 好,本講就到這里。 |