首先总结一下andorid view的键盘事件处理过程:
键盘事件首先交给当前Activity中获得焦点的View,该View的setKeyListener(OnKeyListener l)中设定的l的onKey方法被调用,若返回true,framwork不再处理,否则有framework继续处理.我就是用该方法实现的智能焦点切换。
然后是自动滚动的实现,当ViewGroup中添加了新的View后,不能马上进行滚动,因为此时相应的高度信息还没有更新,需要用Handler来向消息队列投递一个Runnale来进行滚动。
相关实现如下:
1.ScrollView2.java
Java代码
packagecom.google.android.samples.view;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.util.Log;
importandroid.view.KeyEvent;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.ScrollView;
importandroid.widget.TextView;
importcom.google.android.samples.R;
/**
* Demonstrates wrapping a layout in a ScrollView.
*
*/
publicclassScrollView2extendsActivity {
privatestaticfinalString LOG_TAG ="ScrollView2";
privateLinearLayout mLayout;
privateScrollView mScrollView;
privatefinalHandler mHandler =newHandler();
@Override
protectedvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.scroll_view_2);
mLayout = (LinearLayout) findViewById(R.id.layout);
mScrollView = (ScrollView) findViewById(R.id.scrollview);
Button button = (Button) findViewById(R.id.add_widget);
button.setOnClickListener(mClickListener);
//改变默认焦点切换
button.setKeyListener(mAddButtonKeyListener);
}
privateButton.OnClickListener mClickListener =newButton.OnClickListener() {
privateintmIndex =1;
@Override
publicvoidonClick(View arg0) {
// TODO Auto-generated method stub
TextView textView =newTextView(ScrollView2.this);
textView.setText("Text View "+ mIndex);
LinearLayout.LayoutParams p =newLinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
mLayout.addView(textView, p);
Button buttonView =newButton(ScrollView2.this);
buttonView.setText("Button "+ mIndex++);
mLayout.addView(buttonView, p);
//改变默认焦点切换
buttonView.setKeyListener(mNewButtonKeyListener);
//投递一个消息进行滚动
mHandler.post(mScrollToBottom);
}
};
privateRunnable mScrollToBottom =newRunnable() {
@Override
publicvoidrun() {
// TODO Auto-generated method stub
Log.d(LOG_TAG,"ScrollY: "+ mScrollView.getScrollY());
intoff = mLayout.getMeasuredHeight() - mScrollView.getHeight();
if(off >0) {
mScrollView.scrollTo(0, off);
}
}
};
privateView.OnKeyListener mNewButtonKeyListener =newView.OnKeyListener() {
publicbooleanonKey(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.add_widget).requestFocus();
returntrue;
}
returnfalse;
}
};
privateView.OnKeyListener mAddButtonKeyListener =newButton.OnKeyListener() {
@Override
publicbooleanonKey(View v,intkeyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, event.toString());
View viewToFoucus =null;
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
caseKeyEvent.KEYCODE_DPAD_UP:
intiCount = mLayout.getChildCount();
if( iCount >0) {
viewToFoucus = mLayout.getChildAt(iCount -1);
}
break;
caseKeyEvent.KEYCODE_DPAD_DOWN:
if(mLayout.getChildCount() >1) {
viewToFoucus = mLayout.getChildAt(1);
}
break;
default:
break;
}
}
if(viewToFoucus !=null) {
viewToFoucus.requestFocus();
returntrue;
}else{
returnfalse;
}
}
};
}
package com.google.android.samples.view;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.google.android.samples.R;
/**
* Demonstrates wrapping a layout in a ScrollView.
*
*/
public class ScrollView2 extends Activity {
private static final String LOG_TAG = "ScrollView2";
private LinearLayout mLayout;
private ScrollView mScrollView;
private final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.scroll_view_2);
mLayout = (LinearLayout) findViewById(R.id.layout);
mScrollView = (ScrollView) findViewById(R.id.scrollview);
Button button = (Button) findViewById(R.id.add_widget);
button.setOnClickListener(mClickListener);
//改变默认焦点切换
button.setKeyListener(mAddButtonKeyListener);
}
private Button.OnClickListener mClickListener = new Button.OnClickListener() {
private int mIndex = 1;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView textView = new TextView(ScrollView2.this);
textView.setText("Text View " + mIndex);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
mLayout.addView(textView, p);
Button buttonView = new Button(ScrollView2.this);
buttonView.setText("Button " + mIndex++);
mLayout.addView(buttonView, p);
//改变默认焦点切换
buttonView.setKeyListener(mNewButtonKeyListener);
//投递一个消息进行滚动
mHandler.post(mScrollToBottom);
}
};
private Runnable mScrollToBottom = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "ScrollY: " + mScrollView.getScrollY());
int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();
if (off > 0) {
mScrollView.scrollTo(0, off);
}
}
};
private View.OnKeyListener mNewButtonKeyListener = new View.OnKeyListener() {
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.add_widget).requestFocus();
return true;
}
return false;
}
};
private View.OnKeyListener mAddButtonKeyListener = new Button.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, event.toString());
View viewToFoucus = null;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
int iCount = mLayout.getChildCount();
if ( iCount > 0) {
viewToFoucus = mLayout.getChildAt(iCount - 1);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (mLayout.getChildCount() > 1) {
viewToFoucus = mLayout.getChildAt(1);
}
break;
default:
break;
}
}
if (viewToFoucus != null) {
viewToFoucus.requestFocus();
return true;
} else {
return false;
}
}
};
}
2.scroll_view_2.xml
Java代码
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Widget"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Widget"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">