Android 目录:https://blog.csdn.net/dkbnull/article/details/87932847

 

在Android项目中,有时我们需要实现自动滚屏或者根据选择跳转到指定位置的功能。这里用到的主要组件就是滚动视图(ScrollView)

 

以下主要介绍使用ScrollView如何实现界面自动滚动

 

1、声明控件对象

private LinearLayout linearLayout = null;private ScrollView scrollView = null;

2、获得控件对象

linearLayout = (LinearLayout) findViewById(R.id.linearLayout);scrollView = (ScrollView) findViewById(R.id.scrollView);

3、定义一个Handler对象

private final Handler handler = new Handler();

4、实现滚动线程;

private Runnable ScrollRunnable = new Runnable() {@Overridepublic void run() {int off = linearLayout.getMeasuredHeight() - scrollView.getHeight();if (off > 0) {scrollView.scrollBy(0, 30);if (scrollView.getScrollY() == off) {Thread.currentThread().interrupt();} else {handler.postDelayed(this, 1000);}}}};

5、在自动滚动按钮上添加监听器

btnSelf.setOnClickListener(new btnSelfListener());

6、实现自动滚动按钮监听器

/* * 自动滚动按钮监听器 */class btnSelfListener implements OnClickListener {@Overridepublic void onClick(View v) {// 当前按钮文字是自动滚动if (btnSelfStr == R.string.selfMotion) {// 将按钮文字设为“停止滚动”btnSelf.setText(R.string.stopSelfMotion);btnSelfStr = R.string.stopSelfMotion;// 开始自动滚动handler.post(ScrollRunnable);} else {// 将按钮文字设为“自动滚动”btnSelf.setText(R.string.selfMotion);btnSelfStr = R.string.selfMotion;// 停止自动滚动handler.removeCallbacks(ScrollRunnable);}}}

这样我们就实现了布局的自动滚动。那么如何实现根据选择直接跳转到指定的位置呢?

直接跳转主要用到以下方法

// 跳转至开头scrollView.fullScroll(ScrollView.FOCUS_UP);// 跳转至结尾scrollView.fullScroll(ScrollView.FOCUS_DOWN);

1、在跳转按钮上添加监听器

btnGoto.setOnClickListener(new btnGotoListener());

2、实现该监听器

/* * 跳转按钮监听器 */class btnGotoListener implements OnClickListener {int choice = -1;@Overridepublic void onClick(View v) {// 弹出跳转设置对话框new AlertDialog.Builder(MainActivity.this).setTitle("跳转设置").setSingleChoiceItems(new String[] { "开头", "结尾" }, -1,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {switch (which) {case 0:choice = 0;break;case 1:choice = 1;break;}}}).setPositiveButton("跳转",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {switch (choice) {case 0:// 跳转至开头scrollView.fullScroll(ScrollView.FOCUS_UP);break;case 1:// 跳转至结尾scrollView.fullScroll(ScrollView.FOCUS_DOWN);break;}}}).setNegativeButton("返回", null).show();}}

这样我们就实现了布局的自动滚动。

 

 

 

源码:http://download.csdn.net/detail/dkbnull/9298787

 

更多相关文章

  1. android 按钮设计中state_selected属性
  2. Android用户界面设计:基本按钮
  3. android 监控软键盘确定 搜索 按钮并赋予点击事件
  4. Android中图片实现按钮点击效果
  5. android Shape Drawable创建两边半圆的按钮背景
  6. android之【事件监听器】
  7. Android 三个按钮对话框
  8. Android布局学习之――按钮居中
  9. android 多款按钮样式

随机推荐

  1. Android横竖屏切换方法
  2. git 获取android source
  3. Android 消息传递机制分析
  4. Android 创建自定义View 实现TopBar
  5. Android 中如何获取editText文本信息
  6. Android启动脚本init.rc(2)
  7. Android实战简易教程(分享小米手电筒源码)
  8. Android自定义动画类——实现3D旋转动画
  9. android 按钮水波纹效果【背景色】
  10. Android(安卓)数据存取试验 (二)