今天试着使用了一下Android的滚轮 以下是一个小小的测试 读取测试文件

主要是使用scrollTo函数和getScrollY()

程序点击BUTTON按钮 则向下滚到2222处 并显示

main.xml

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

android:id="@+id/widget38"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

>

<LinearLayout

android:id="@+id/widget32"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_x="0px"

android:layout_y="0px"

>

</LinearLayout>

<ScrollView

android:id="@+id/sv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_x="0px"

android:layout_y="20px"

>

<TextView

android:id="@+id/txtView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="hello,user!"

android:textSize="20sp"

>

</TextView>

</ScrollView>

<Button

android:id="@+id/test"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button"

android:layout_x="230px"

android:layout_y="2px"

>

</Button>

</AbsoluteLayout>

main.java package zy.Scroller; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.http.util.EncodingUtils; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; public class main extends Activity { /** Called when the activity is first created. */ final public String DEV_FILE = "/data/data/zy.Scroller/aaa.txt";//测试文件 final String TEXT_ENCODING = "UTF-8"; ScrollView sv; TextView tv; public Button test; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sv=(ScrollView)findViewById(R.id.sv); tv=(TextView)findViewById(R.id.txtView); String str; str=getinfo(DEV_FILE); String [] x; x=str.split("/r"); tv.setText(x[0]); int i; for(i=1;i<=x.length-1;i++) { tv.append(x[i]); } test=(Button)findViewById(R.id.test); test.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub sv.scrollTo(0, 2222); DisplayToast(sv.getScrollY()+""); } }); } protected void onScrollChanged (int l, int t, int oldl, int oldt) { DisplayToast("123"); } public String getinfo(String path) { File file; String str=""; FileInputStream in; try{ //打开文件file的InputStream file = new File(path); in = new FileInputStream(file); //将文件内容全部读入到byte数组 int length = (int)file.length(); byte[] temp = new byte[length]; in.read(temp, 0, length); //将byte数组用UTF-8编码并存入display字符串中 str = EncodingUtils.getString(temp,TEXT_ENCODING); //关闭文件file的InputStream in.close(); } catch (IOException e) { DisplayToast(e.toString()); } return str; } public void DisplayToast(String str) { Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); } }

更多相关文章

  1. Android程序调试时生成main.out.xml文件
  2. Android集成银联支付,提示java.lang.UnsatisfieldLinkError错误
  3. Android(安卓)文字链接 文字点击时的背景颜色
  4. Android的CheckBox控件的点击效果布局文件
  5. 读取android手机流量信息
  6. Android(安卓)混淆提示 java.io.FileNotFoundException: ...\pr
  7. Android系统在超级终端下必会的命令大全(adb shell命令大全)
  8. 箭头函数的基础使用
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android与H5互调使用cordova的WebView,插
  2. Android 时间获取及格式化
  3. Android进度条、自动提示框、下拉框动态
  4. android studio教程 – 快捷键
  5. android中添加arial字体(非android默认字
  6. Android(安卓)Studio如何设置代码自动提
  7. android 在程序中开启GPS功能
  8. Android—构建安全的Android客户端请求,避
  9. GooglePlay 分包 安装obb
  10. 在android中创建bitmap避免内存不足的方