![](/img/trans.png)
[英]Android how to make scrollView auto scroll to the bottom when click on editText?
[英]Android, How to Scroll Programmatically a scrollview When I Clicked on edittext
當ı單擊edittext
,由於鍵盤,用戶看不到我的button
。
所以我想,當用戶單擊edittext
, scrollview
絕對會平滑地移到上方。
我的第一個問題我應該為此使用哪個聽眾? OnClick
, OnFocus
, OnTouch
等?
我嘗試了此代碼塊,但需要雙擊才能滾動它。 首先單擊打開鍵盤,然后單擊滾動到scrollview
。
我的第二個問題有人對這個問題有想法嗎? 感謝您的幫助。
scrollView = (ScrollView) findViewById(R.id.scrollview);
et_space.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
scrollView.smoothScrollTo(0, 500);
return false;
}
});
謝謝大家的所有回答,但我自己解決了問題,
首先,我想說您必須使用“ OnFocusListener”,然后必須使用類似的Handler。
現在,當我單擊編輯文本時; 鍵盤已打開,scrollview同時滾動到頂部:)
et_space.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
final Handler handler;
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
scrollView.smoothScrollTo(0, 500);
}
};
handler.postDelayed(r, 200);
}
}
});
嘗試這個
在EditText的OnClickListener中使用此代碼
scrollview.scrollBy(0,scrollview.getBottom());
sv.scrollTo(5, 10);
希望這會幫助你。
scrollView.post(new Runnable() {
@Override
public void run() {
sv.scrollTo(5, 10);
}
});
像這樣修改您的代碼:
et_space.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
scrollView.smoothScrollTo(0, 500);
return false;
}
}
return true;
}
});
您可以在EditText下的同一位置添加小部件。例如,名為“ btn_show_keyboard”的ImageView。
然后在您的btn_show_keyboard.onClick()方法中執行此方法。
public void showSoftInput() {
mEditText.requestFocus();
mInputManager.showSoftInput(mEditText, 0);
}
在此softinputmode中設置目標活動。
android:windowSoftInputMode="adjustResize"
試試看這些,希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.