Android scrollview usage

Source: Internet
Author: User

Today, I tried to use the following Android scroll wheel to read the test file from a small test.

It mainly uses the scrollto function and getscrolly ()

The program click the button to scroll down to 2222 and display

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/ZY. scroller/aaa.txt "; // test file 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); tv1_(textview1_findviewbyid(r.id.txt view); 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; string STR = ""; fileinputstream in; try {// enable inputstream file of file = new file (PATH ); in = new fileinputstream (File); // read all the file content to the byte array int length = (INT) file. length (); byte [] temp = new byte [length]; In. read (temp, 0, length); // encode the byte array with a UTF-8 and store it in the display string STR = encodingutils. getstring (temp, text_encoding); // close the inputstream in. close ();} catch (ioexception e) {displaytoast (E. tostring ();} return STR;} public void displaytoast (string Str) {toast. maketext (this, STR, toast. length_short ). show ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.