拖动条 SeekBar 实例
2016-09-04 15:59
447 查看
(1)在XML文件里,添加一个TextView用以显示拖动条数据,添加一个SeekBar组件(即拖动条),SeekBar的android:max,用以规定最大值;android:progress,用以规定初始值,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:text="当前值: 0" />
<!-- 拖动条-->
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar1"
android:max="100"
android:progress="0"
android:padding="10dp"
/>
</LinearLayout>
(2)在MainActivity中,使用SeekBar的setOnSeekBarChangeListener事件监听器,重写onStopTrackingTouch()和onStartTrackingTouch(r)输出状态,onProgressChanged()方法获取进度条进度值,如下:
package com.example.onseekbarchangelistener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private SeekBar seekBar ;//拖动条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView result =(TextView) findViewById(R.id.textView1);
seekBar=(SeekBar) findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "结束滑动", Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "开始滑动", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
result.setText("当前值:"+progress);
String resultLog = result.getText().toString();
Log.d("seekBar",resultLog);
}
});
}
}
(3)运行效果,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:text="当前值: 0" />
<!-- 拖动条-->
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar1"
android:max="100"
android:progress="0"
android:padding="10dp"
/>
</LinearLayout>
(2)在MainActivity中,使用SeekBar的setOnSeekBarChangeListener事件监听器,重写onStopTrackingTouch()和onStartTrackingTouch(r)输出状态,onProgressChanged()方法获取进度条进度值,如下:
package com.example.onseekbarchangelistener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private SeekBar seekBar ;//拖动条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView result =(TextView) findViewById(R.id.textView1);
seekBar=(SeekBar) findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "结束滑动", Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "开始滑动", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
result.setText("当前值:"+progress);
String resultLog = result.getText().toString();
Log.d("seekBar",resultLog);
}
});
}
}
(3)运行效果,如下:
相关文章推荐
- 【牛客网】矩阵中的路径
- MyBatis学习之三:动态SQL语句
- GDB调试程序
- [Lintcode]Container With Most Water
- Android Dialog的 WindowLeaked窗体泄露异常
- spring的事务配置详解
- SVM边学边总结系列——非线性可分情况
- 数组形参2
- 期货交易流程
- POJ 2236 Wireless Network 【并查集】
- JavaSrcipt的Object.keys()
- Java se项目:记事本
- 网站前端_JavaScript-基础入门.0003.JavaScript语法变量
- oracle 执行计划学习
- 约瑟夫问题JAVA算法
- Android工具类--获取屏幕信息类
- Codeforces Round #359 (Div. 2) A
- Nim Game——Difficulty:Easy
- LeetCode 题目总结/分类
- 数组形参