Code
Packagecom.lxt008;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.view.KeyEvent;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.ScrollView;ImportAndroid.widget.TextView; Public classActivity01extendsactivity{PrivateLinearLayout mlayout; PrivateScrollView Mscrollview; Private FinalHandler Mhandler =NewHandler (); /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //Create a linear layoutMlayout =(LinearLayout) Findviewbyid (r.id.layout); //Create a ScrollView objectMscrollview =(ScrollView) Findviewbyid (R.ID.SCROLLVIEW01); Button Button=(Button) Findviewbyid (R.ID.BUTTON01); Button.setonclicklistener (Mclicklistener); //Change the default focus toggleButton.setonkeylistener (Maddbuttonkeylistener); } //Button Event Listener//Add a TextView and button when you click the button PrivateButton.onclicklistener Mclicklistener =NewButton.onclicklistener () {Private intMindex = 1; @Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubTextView TextView =NewTextView (Activity01. This); Textview.settext ("Text View" +Mindex); Linearlayout.layoutparams P=Newlinearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT, Linearl Ayout. Layoutparams.wrap_content); //Add a TextView to the linear layoutMlayout.addview (TextView, p); Button Buttonview=NewButton (Activity01. This); Buttonview.settext ("Button" + mindex++); //Add a button to the linear layoutMlayout.addview (Buttonview, p); //Change the default focus toggleButtonview.setonkeylistener (Mnewbuttonkeylistener); //post a message to scrollMhandler.post (Mscrolltobottom); } }; PrivateRunnable Mscrolltobottom =NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub intOff = Mlayout.getmeasuredheight ()-mscrollview.getheight (); if(Off > 0) {Mscrollview.scrollto (0, off); } } }; //Event Monitoring PrivateView.onkeylistener Mnewbuttonkeylistener =NewView.onkeylistener () { Public BooleanOnKey (View V,intKeyCode, KeyEvent event) { if(KeyCode = = Keyevent.keycode_dpad_down &&event.getaction ()= = Keyevent.action_down &&v= = Mlayout.getchildat (Mlayout.getchildcount ()-1) {Findviewbyid (r.id.button01). Requestfocus (); return true; } return false; } }; //Event Monitoring PrivateView.onkeylistener Maddbuttonkeylistener =NewButton.onkeylistener () {@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) { //TODO auto-generated Method StubView Viewtofoucus =NULL; if(event.getaction () = =Keyevent.action_down) { intICount =Mlayout.getchildcount (); Switch(keycode) { Casekeyevent.keycode_dpad_up:if(ICount > 0) {Viewtofoucus= Mlayout.getchildat (iCount-1); } Break; CaseKeyevent.keycode_dpad_down:if(ICount <mlayout.getweightsum ()) {Viewtofoucus= Mlayout.getchildat (ICount + 1); } Break; default: Break; } } if(Viewtofoucus! =NULL) {viewtofoucus.requestfocus (); return true; } Else { return false; } } }; }
Layout file
<ScrollViewxmlns: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"> <LinearLayoutAndroid:id= "@+id/layout"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"> <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "TextView0"/> <ButtonAndroid:id= "@+id/button01"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Button0"/> </LinearLayout></ScrollView>
Android-scrollview scrolling trying to Demo