SeekBar拖動條的應用實例
更新時間:2020年10月23日 15:41:27 作者:小龍
這篇文章主要為大家詳細介紹了SeekBar拖動條的應用實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了SeekBar拖動條的應用代碼,供大家參考,具體內(nèi)容如下
目標效果
在該頁面中放一個拖動條的狀態(tài)提示信息,一個拖動條以及一個顯示拖動條值的信息。當我們點擊拖動條時,在狀態(tài)欄顯示:正在拖動,并顯示此時拖動條的值;當停止點擊拖動條的時候,狀態(tài)顯示:停止拖動。
目標界面如下所示:



頁面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/b1" >
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="我是當前拖動條的狀態(tài)"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textSize="24dp"/>
<SeekBar
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/show_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="當前的值為0"
android:textColor="#FFFFFF"
android:textSize="24dp"/>
</LinearLayout>
動作響應事件
package com.example.seekbar;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView status,show;
SeekBar seek=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status=(TextView) findViewById(R.id.status);
show=(TextView) findViewById(R.id.show_values);
seek=(SeekBar) findViewById(R.id.seek);
//為拖動條添加事件監(jiān)聽
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
//當檢測到拖動條停止滑動時的一個方法
status.setText("停止拖動");
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
//當檢測到拖動條開始滑動,第一次滑動
status.setText("開始滑動");
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2)
{
//當檢測到拖動條開始位置改變的一個方法,其中arg1表示當前滑動的values
status.setText("正在滑動");
show.setText("當前的值為:"+arg1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 雙擊Back鍵退出應用的實現(xiàn)方法
這篇文章主要介紹了Android 雙擊Back鍵退出應用的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法
這篇文章主要介紹了Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法,以實例形式較為詳細的分析了Android定制啟動界面的布局及功能實現(xiàn)相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
詳解Android中的ActivityThread和APP啟動過程
ActivityThread就是我們常說的主線程或UI線程,ActivityThread的main方法是整個APP的入口,本篇深入學習下ActivityThread,順便了解下APP和Activity的啟動過程。2021-06-06

