Android開發(fā)中怎么實現(xiàn)一個拖動條和評星條的功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
10年積累的網(wǎng)站制作、成都網(wǎng)站制作經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有都勻免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
一 拖動條
安卓手機音量設(shè)置都是給出一個拖動條,使得用戶能夠拖動滑塊進行設(shè)置,這里我們介紹拖動條。
安卓拖動條控件是繼承自ProgressBar控件,所以它能夠支持ProgressBar的xml屬性。但是他有自己的獨特屬性:
android:max 設(shè)置最大的拖動兩
android:progress 設(shè)置初始化進度
android:thumb 設(shè)置滑塊圖形
事件監(jiān)聽方面,拖動條需要注意:我們不在監(jiān)聽用戶的點擊操作,而是監(jiān)聽滑塊的改變,下面用一個實例簡單的操作一下拖動條。
實例:界面上給出一個拖動條和文本,滑動滑塊文本動態(tài)顯示
1.新建工程,在布局文件中加入一個文本和拖動條。這里我設(shè)置了當前進度值和滑塊圖形
<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="當前進度: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.代碼中獲取文本和拖動條,給拖動條加監(jiān)聽器。監(jiān)聽器內(nèi)部控制了文本的動態(tài)顯示。監(jiān)聽有三個方法,注意:改寫一下開始和結(jié)束滑動的方法,另外一個和是否是用戶滑動有關(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("當前進度:" + p);
}//結(jié)束滑動
@Override
public void onStartTrackingTouch(SeekBar s) {
tv.setText("正在拖動!");
}//開始滑動
@Override
public void onProgressChanged(SeekBar s, int arg1, boolean arg2) {
}
});運行代碼,滑動滑塊,能夠看到文本根據(jù)滑動動態(tài)的顯示內(nèi)容。

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

看完上述內(nèi)容,你們掌握Android開發(fā)中怎么實現(xiàn)一個拖動條和評星條的功能的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當前標題:Android開發(fā)中怎么實現(xiàn)一個拖動條和評星條的功能
鏈接地址:http://www.chinadenli.net/article44/piiihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、定制網(wǎng)站、網(wǎng)站改版、App開發(fā)、網(wǎng)站營銷、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)