首先是JAVA主代碼:
package com.dudu.djy;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
/***
* scrollview
* @author dujinyang
*
*/
public class ScrollViewTests extends Activity {
/** Called when the activity is first created. */
private LinearLayout mLayout;
private ScrollView scView;
private final Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化操作
mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);
scView= (ScrollView) this.findViewById(R.id.ScrollView);
Button mBtn = (Button) this.findViewById(R.id.Button);
mBtn.setOnClickListener(mClickListener);// 添加點擊事件監聽
}
//監聽返回事件 可以不要
public boolean onKeyDown(int keyCode, KeyEvent event){
Button bt = (Button) this.getCurrentFocus();
int count = mLayout.getChildCount();
Button bm = (Button) mLayout.getChildAt(count-1);
if(keyCode==KeyEvent.KEYCODE_DPAD_UP && bt .getId()==R.id.Button){
bm.requestFocus();
return true;
}else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && bt .getId()==bm.getId()){
this.findViewById(R.id.Button).requestFocus(); //取消焦點
return true;
}
return false;
}
// Button事件監聽,當點擊第一個按鈕時增加一個button和一個textview
//這裡只是做個增加按鈕和資料的
private Button.OnClickListener mClickListener = new Button.OnClickListener() {
private int index = 1;
@Override
public void onClick(View v) {
TextView tv= new TextView(ScrollViewTest.this);//定義一個TextView
tView.setText("TextView" + index);//設定TextView的文本資訊
//設定線性布局的屬性
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(tv, params);//添加一個TextView控制項
Button button = new Button(ScrollViewTest.this);//定義一個Button
button.setText("Button" + index);//設定Button的文本資訊
button.setId(index++);//id
mLayout.addView(button, params);//添加一個Button控制項
mHandler.post(mScrollToButton);//傳遞一個訊息進行滾動
}
};
//傳遞一個訊息進行滾動
private Runnable mScrollToButton = new Runnable() {
@Override
public void run() {
int off = mLayout.getMeasuredHeight() - scView.getHeight();
if (off > 0) {
scView.scrollTo(0, off);//改變捲軸的位置
}
}
};
}
然後是main.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<!--初始化時的資料-->
<LinearLayout android:id="@+id/LinearLayout"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/TestView" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="TestView0" />
<Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</ScrollView>