我在android中遇到键盘和edittext的问题。 我的情况:
更新
布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:singleLine="true" />
</LinearLayout>
</ScrollView>
和manifest.xml
<activity
android:name="vn.com.fss.vndirect.TestActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我的问题:为什么edittext不像第一次那样向上移动,以及如何解决它?
由于
答案 0 :(得分:1)
使用此自定义Edittext类。按下后退按钮时,它将删除Edittext焦点
public class KeyboardEditText extends EditText
{
public KeyboardEditText(Context context) {
super(context);
}
public KeyboardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setOnTouchListener(OnTouchListener l) {
super.setOnTouchListener(l);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (listener != null)
listener.onStateChanged(this, true);
}
@Override
public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_UP) {
if (listener != null)
listener.onStateChanged(this, false);
// Hide cursor
setFocusable(false);
// Set EditText to be focusable again
setFocusable(true);
setFocusableInTouchMode(true);
}
return super.onKeyPreIme(keyCode, event);
}
/**
* Keyboard Listener
*/
KeyboardListener listener;
public void setOnKeyboardListener(KeyboardListener listener) {
this.listener = listener;
}
public interface KeyboardListener {
void onStateChanged(KeyboardEditText keyboardEditText, boolean showing);
}
}
答案 1 :(得分:0)
解决此问题的一种方法是通过在scrollView
内的布局文件中插入主布局来使布局可滚动。尝试一下,如果它不起作用,请告诉我。例如:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--Your Layout content-->
</LinearLayout>
</ScrollView>
每当显示键盘时,这基本上会在键盘上方向上滚动editText
。