浏览 11289 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-12-20
键盘事件首先交给当前Activity中获得焦点的View,该View的setKeyListener(OnKeyListener l)中设定的l的onKey方法被调用,若返回true,framwork不再处理,否则有framework继续处理.我就是用该方法实现的智能焦点切换。 然后是自动滚动的实现,当ViewGroup中添加了新的View后,不能马上进行滚动,因为此时相应的高度信息还没有更新,需要用Handler来向消息队列投递一个Runnale来进行滚动。 相关实现如下: 1.ScrollView2.java 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 <?xml version="1.0" encoding="utf-8"?> <!-- Demonstrates scrolling with a ScrollView. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button id="@+id/add_widget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add Widget"/> <ScrollView id="@+id/scrollview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical"> <LinearLayout id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> </LinearLayout> </ScrollView> </LinearLayout> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-12-21
不错啊!绝对要支持!
|
|
返回顶楼 | |