Android入门第18天-Android里的SeekBar的使用

Posted TGITCIC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android入门第18天-Android里的SeekBar的使用相关的知识,希望对你有一定的参考价值。

SeekBar的介绍

SeekingBar是这么样的一个东西,它常常用在播放视频、音步时用来调节音量、模糊阀值等场景。

SeekBar里怎么是通过:android:layout_weight="1"这个值设置大小的。

其它我们都使用默认的就行。

我们先来看UI界面的代码吧。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
 
    <TableRow>
 
        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_weight="10"
            android:layout_height="wrap_content" />
    </TableRow>
 
    <TableRow>
 
        <TextView
            android:id="@+id/seekBarValueTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="" />
    </TableRow>
</TableLayout>

 

  • 主界面里我们放了一个SeekBar,放了一个TextView;
  • 每次我们在SeekBar上拖动滑块时,我们会在Textview里显示当前SeekBar的值;

来看后台交互代码的实现。

MainActivity.java

package org.mk.android.demo;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity 
    private SeekBar seekBar;
    private TextView seekBarValueTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try 
            initSeekBar();
        catch(Exception e)
            Log.e("app","initSeekBar() error: "+e.getMessage(),e);
        
    
    private void initSeekBar() 
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBarValueTextView = (TextView) findViewById(R.id.seekBarValueTextView);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
                StringBuffer seekValueSB = new StringBuffer();
                seekValueSB.append("current progress is").append(" ").append(String.valueOf(progress)).append(" /100");
                seekBarValueTextView.setText(seekValueSB.toString());
            
 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) 
                Log.i("app","你按住了SeekBar");
            
 
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) 
                Log.i("app", "你放开了SeekBar");
            
        );
    

请自己动一下手,然后试试看效果如何吧。

以上是关于Android入门第18天-Android里的SeekBar的使用的主要内容,如果未能解决你的问题,请参考以下文章

Android入门第17天-Android里的ProgressBar的使用

Android入门第16天-Android里的SwitchButton的使用

Android入门第30天-Android里的Toast的使用

Android入门第19天-Android里的RatingBar的使用

Android入门第20天-Android里的ScrollView的使用

Android入门第33天-Android里的弹出式对话框