Android高級(jí)界面組件之拖動(dòng)條和評(píng)星條的功能實(shí)現(xiàn)
一 拖動(dòng)條
安卓手機(jī)音量設(shè)置都是給出一個(gè)拖動(dòng)條,使得用戶能夠拖動(dòng)滑塊進(jìn)行設(shè)置,這里我們介紹拖動(dòng)條。
安卓拖動(dòng)條控件是繼承自ProgressBar控件,所以它能夠支持ProgressBar的xml屬性。但是他有自己的獨(dú)特屬性:
android:max 設(shè)置最大的拖動(dòng)兩
android:progress 設(shè)置初始化進(jìn)度
android:thumb 設(shè)置滑塊圖形
事件監(jiān)聽(tīng)方面,拖動(dòng)條需要注意:我們不在監(jiān)聽(tīng)用戶的點(diǎn)擊操作,而是監(jiān)聽(tīng)滑塊的改變,下面用一個(gè)實(shí)例簡(jiǎn)單的操作一下拖動(dòng)條。
實(shí)例:界面上給出一個(gè)拖動(dòng)條和文本,滑動(dòng)滑塊文本動(dòng)態(tài)顯示
1.新建工程,在布局文件中加入一個(gè)文本和拖動(dòng)條。這里我設(shè)置了當(dāng)前進(jìn)度值和滑塊圖形
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="112dp" android:text="當(dāng)前進(jìn)度:0" /> <SeekBar android:id="@+id/seekBar1" android:thumb="@drawable/penguin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" /> </RelativeLayout>
2.代碼中獲取文本和拖動(dòng)條,給拖動(dòng)條加監(jiān)聽(tīng)器。監(jiān)聽(tīng)器內(nèi)部控制了文本的動(dòng)態(tài)顯示。監(jiān)聽(tīng)有三個(gè)方法,注意:改寫(xiě)一下開(kāi)始和結(jié)束滑動(dòng)的方法,另外一個(gè)和是否是用戶滑動(dòng)有關(guān),我們且不去管它
tv = (TextView)findViewById(R.id.textView1);
sb = (SeekBar)findViewById(R.id.seekBar1);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar s) {
final int p = s.getProgress();
tv.setText("當(dāng)前進(jìn)度:" + p);
}//結(jié)束滑動(dòng)
@Override
public void onStartTrackingTouch(SeekBar s) {
tv.setText("正在拖動(dòng)!");
}//開(kāi)始滑動(dòng)
@Override
public void onProgressChanged(SeekBar s, int arg1, boolean arg2) {
}
});
運(yùn)行代碼,滑動(dòng)滑塊,能夠看到文本根據(jù)滑動(dòng)動(dòng)態(tài)的顯示內(nèi)容。

二 評(píng)星條
很多視屏軟件和是應(yīng)用市場(chǎng)軟件都有評(píng)星的功能,這是的評(píng)分應(yīng)用場(chǎng)景是:拖動(dòng)評(píng)星條,之后點(diǎn)擊某個(gè)提交按鈕完成評(píng)分。這里我們簡(jiǎn)單看一下評(píng)星條的屬性,之后模擬一個(gè)類似的評(píng)星功能。
android:isIndicator 表明是指示器,也就是能不能被用戶評(píng)分,值為"true"不能被改變
android:numStars 評(píng)星條的星星總數(shù)
android:rating 評(píng)星條的默認(rèn)星級(jí)
android:stepSize 評(píng)星一次變化的分量,默認(rèn)狀態(tài)下為0.5,用戶一次拖動(dòng)改變0.5的星級(jí)
實(shí)例:做一個(gè)簡(jiǎn)單的評(píng)分界面
1.新建工程,布局中加入顯示文本,評(píng)星條,提交按鈕
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="184dp" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/ratingBar1" android:layout_centerHorizontal="true" android:layout_marginBottom="100dp" android:text="評(píng)分:" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ratingBar1" android:layout_centerHorizontal="true" android:text="提交" /> </RelativeLayout>
2.在主Activity里面實(shí)例化顯示文本,評(píng)星條,按鈕
tv = (TextView)findViewById(R.id.textView1); asb = (RatingBar)findViewById(R.id.ratingBar1); b = (Button)findViewById(R.id.button1);
3.給按鈕加監(jiān)聽(tīng)事件,獲取評(píng)星條的評(píng)分,顯示到文本中去。評(píng)星的獲取通過(guò)getRating()方法
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final float r;
r = asb.getRating();
tv.setText("評(píng)分"+ r +"星");
}
});
運(yùn)行代碼,效果如下:

以上所述是小編給大家介紹的Android高級(jí)界面組件之拖動(dòng)條和評(píng)星條的功能實(shí)現(xiàn),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
android自定義控件和自定義回調(diào)函數(shù)步驟示例
這篇文章主要介紹了android自定義控件步驟示例,包括為View類增加屬性、響應(yīng)用戶消息、自定義回調(diào)函數(shù)等方法2014-01-01
Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Android實(shí)現(xiàn)頁(yè)面翻轉(zhuǎn)和自動(dòng)翻轉(zhuǎn)功能
這篇文章主要介紹了Android中簡(jiǎn)單實(shí)現(xiàn)頁(yè)面翻轉(zhuǎn)和自動(dòng)翻轉(zhuǎn)的功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Android統(tǒng)一處理登錄后攜帶數(shù)據(jù)跳轉(zhuǎn)到目標(biāo)頁(yè)面的方式
我們?cè)陂_(kāi)發(fā)的時(shí)候,一定會(huì)遇到頁(yè)面跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一處理登錄后攜帶數(shù)據(jù)跳轉(zhuǎn)到目標(biāo)頁(yè)面的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
android仿即刻點(diǎn)贊文字部分的自定義View的示例代碼
本篇文章主要介紹了android仿即刻點(diǎn)贊文字部分的自定義View的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁(yè)面
這篇文章主要介紹了Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁(yè)面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android事件分發(fā)機(jī)制(上) ViewGroup的事件分發(fā)
這篇文章主要為大家詳細(xì)介紹了Android ViewGroup的事件分發(fā)機(jī)制上篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

