Android ScrollView用法

來源:互聯網
上載者:User

今天試著使用了一下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.javapackage 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();    }  }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.