今天就跟大家聊聊有關SeekBar拖動條的使用方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
目標效果
在該頁面中放一個拖動條的狀態提示信息,一個拖動條以及一個顯示拖動條值的信息。當我們點擊拖動條時,在狀態欄顯示:正在拖動,并顯示此時拖動條的值;當停止點擊拖動條的時候,狀態顯示:停止拖動。
目標界面如下所示:
頁面布局
<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="我是當前拖動條的狀態" 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); //為拖動條添加事件監聽 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; } }
看完上述內容,你們對SeekBar拖動條的使用方法有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。