[英]Android EditText's container (RecyclerView) auto scroll when EditText is Focused
[英]android auto scroll when clicked on edittext
當用戶單擊edittext時,我希望屏幕滾動以在屏幕中間顯示edittext。 所以我試着聽觸摸(setOnTouchListener,也嘗試過onClick和onFocuse),然后試聽平滑滾動到屏幕,將edittext放在屏幕中間。
但是由於某種原因,當我將setOnTouchListener添加到edittext時,它根本無法獲得焦點。
我需要解決什么? 或如何實現?
這是setOnTouchListener代碼,該代碼導致單擊時不會使edittext聚焦:
et_email.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ScrollView scrollView = (ScrollView)getView().findViewById(R.id.ScrollViewSendDetails);
scrollView.smoothScrollTo(0, et_email.getBottom());
return true;
}
});
從onTouch()
方法返回true
表示此處正在使用touch事件。 為了使事件傳播到View自己的觸摸偵聽器,您需要return false;
。
為了使ScrollView
完成滾動后獲得EditText
焦點,您可以將Runnable
到ScrollView
的處理程序,以請求將焦點集中在EditText
。 例如:
et_email.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
...
scrollView.smoothScrollTo(0, et_email.getTop());
scrollView.post(new Runnable() {
@Override
public void run() {
et_email.requestFocus();
}
}
);
return false;
}
}
);
不幸的是,輸出程序仍要雙擊滾動
final 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);
scrollView.post(new Runnable() {
@Override
public void run() {
et_space.requestFocus();
}
});
return false;
}
});
首先,我想說您必須使用“ OnFocusListener”,然后必須使用類似的Handler。
現在,當我單擊編輯文本時; 鍵盤已打開,scrollview同時滾動到頂部:)
et_space.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final Handler handler;
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
scrollView.smoothScrollTo(0, 500);
handler.postDelayed(this, 200);
}
};
handler.postDelayed(r, 200);
}
});
在onCreate方法上使用此行代碼。 單擊EditText時,它可以自動滾動:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.