Android--scrollview

Source: Internet
Author: User

1.activity_scrollview.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/scrollview01"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:scrollbars= "None"
>

<linearlayout
Android:id= "@+id/linearlayout01"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >

<textview
Android:id= "@+id/textview1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "TextView0"/>

<button
Android:id= "@+id/button001"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Button0"/>

</LinearLayout>


</ScrollView>
2, Scrollviewactivity

public class Scrollviewactivity extends Activity {
Private LinearLayout mlayout;
Private ScrollView Mscrollview;
Private final Handler Mhandler = new Handler ();

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_scrollview);
Mlayout = (linearlayout) Findviewbyid (r.id.linearlayout01);
Mscrollview = (ScrollView) Findviewbyid (R.ID.SCROLLVIEW01);
Button button = (button) Findviewbyid (r.id.button001);
Set Button click events
button.Setonclicklistener(Mclicklistener);
Change the default focus toggle
button.Setonkeylistener(Maddbuttonkeylistener);
}

Button event Listener, when clicking the button, add a TextView and button
Private Button.onclicklistener Mclicklistener = new Button.onclicklistener () {
private int mindex = 1;

@Override
public void OnClick (View v) {
TextView TextView = new TextView (scrollviewactivity.this);
Textview.settext ("Text View:" + mindex);
Linearlayout.layoutparams params = new Linearlayout.layoutparams (
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Add a TextView to the linear layout
Mlayout.addview (TextView, params);
Button Buttonview = New button (scrollviewactivity.this);
Buttonview.settext ("button:" + mindex++);
Add a button to the linear layout
Mlayout.addview (Buttonview, params);
Change the default focus toggle **************************
Buttonview.Setonkeylistener(Mnewbuttonkeylistener);
Post a message to scroll
Mhandler.post (mscrolltobuttom);
}
};

Private Runnable Mscrolltobuttom = new Runnable () {

@Override
public void Run () {
int off = Mlayout.getmeasuredheight ()-mscrollview.getheight ();
if (off > 0) {
Mscrollview.scrollto (0, off);
}
}
};
Event Monitoring
Private View.onkeylistener Mnewbuttonkeylistener = new View.onkeylistener () {

@Override
public boolean OnKey (View v, int keycode, keyevent event) {
if (keycode = =KeyEvent. Keycode_dpad_down
&& event.getaction () = = Keyevent.action_down
&& v = = Mlayout.getchildat (Mlayout.getchildcount ()-1)) {
Findviewbyid (r.id.button001).Requestfocus();
return true;
}
return false;
}
};
Event Monitoring
Private View.onkeylistener Maddbuttonkeylistener = new Button.onkeylistener () {

@Override
public boolean OnKey (View v, int keycode, keyevent event) {
View viewtofocus = null;
if (event.getaction () = = Keyevent.action_down) {
int iCount = Mlayout.Getchildcount();
Switch (keycode) {
Case KEYEVENT.KEYCODE_DPAD_UP:
if (ICount > 0) {
Viewtofocus = Mlayout.getchildat (iCount-1);
}
Break
Case Keyevent.keycode_dpad_down:
if (ICount < mlayout.Getweightsum()) {
Viewtofocus = Mlayout.getchildat (ICount + 1);
}
Break

Default
Break
}
}
if (viewtofocus! = null) {
Viewtofocus.Requestfocus();
return true;
} else {
return false;
}
}
};

}

Android--scrollview

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.