2010-04-19 110 views
4

我有文本区域和向下的“ok”和“cancel”按钮。 当我点击文本区域键盘出现,并专注于文本区域和按钮隐藏在键盘后面。android向下滚动

我希望当文本区域焦点滚动一点点,并显示底部按钮,而文本区域ID选中。

回答

8

我固定它通过自己:d以下是代码

我称为以下的方法时文本框获得焦点

private final void focusOnButtons(){ 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       ScrollView sv = (ScrollView)findViewById(R.id.scrl); 
       sv.scrollTo(0, sv.getBottom()); 
      } 
     },1000); 
    } 
4

你可以尝试在滚动型包装现有的布局:
https://developer.android.com/reference/android/widget/ScrollView.html
您的布局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="wrap_content" 
     android:layout_height="wrap_content"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <EditText android:text="@string/hello" android:id="@+id/EditText01" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     </EditText> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <Button android:text="@+id/Button01" android:id="@+id/Button01" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      </Button> 
      <Button android:text="@+id/Button02" android:id="@+id/Button02" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      </Button> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

评论回应:
你可能想看看这个页面:
https://developer.android.com/training/keyboard-input/index.html

我想你想要的结果可以通过移动滚动查看外的按钮并在活动上设置android:windowSoftInputMode="adjustResize"来完成。

+0

我已经使用了滚动视图,但是当出现键盘时它不滚动到显示按钮。 – 2010-04-20 11:27:49

0

我会从溶液调整几件事情:

1 .-把smoothScroll代替硬盘滚动

2.-将延迟从1000减少到300(足够)

OnFocusChangeListener onFocus = new OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        scrollView.smoothScrollTo(0, scrollView.getBottom()); 
       } 
      },300); 
     } 
    };