我有一个如下所示的布局,当用户点击EditText时,我希望有以下行为:
未达到要求:
答案 0 :(得分:0)
我不确定是否有任何开箱即用的解决方案可以让它按预期工作。
但是,您可以使用windowSoftInputMode = adjustNothing
来实现指定的行为
并设置焦点侦听器以编辑文本,并在编辑文本获得焦点时滚动滚动视图。
如果不清楚如何实现滚动,我可以添加一个例子。
答案 1 :(得分:0)
我认为没有非常简单的方法来实现这一目标。但有一个项目here,当键盘打开时会弹出SizeNotifierRelativeLayout。
答案 2 :(得分:0)
将父布局更改为相对布局
public class SizeNotifierRelativeLayout extends RelativeLayout {
private Rect rect = new Rect();
public SizeNotifierRelativeLayoutDelegate delegate;
public abstract interface SizeNotifierRelativeLayoutDelegate {
public abstract void onSizeChanged(int keyboardHeight);
}
public SizeNotifierRelativeLayout(Context context) {
super(context);
}
public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs) {
super(context, attrs);
}
public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Calculate the soft keyboard height and report back to listener
* @param changed
* @param l
* @param t
* @param r
* @param b
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (delegate != null) {
View rootView = this.getRootView();
int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView);
this.getWindowVisibleDisplayFrame(rect);
int keyboardHeight = usableViewHeight - (rect.bottom - rect.top);
delegate.onSizeChanged(keyboardHeight);
}
}
您的活动或片段的下一个包含此代码
private SizeNotifierRelativeLayout sizeNotifierRelativeLayout;
View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = view.findViewById(R.id.root);
sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout)
view.findViewById(R.id.chat_layout);
sizeNotifierRelativeLayout.delegate = this;
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int screenHeight = rootView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
Log.d("Height", "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, keypadHeight - 200);
}
} else {
// keyboard is closed
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.post(new Runnable() {
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, 0);
}
}
});
}
try {
}
} catch (NullPointerException e) {
e.printStackTrace();
System.out.print(e.getMessage());
}
}
}
});
}
答案 3 :(得分:0)
我执行了以下操作来解决此问题。
AndroidManifest文件活动配置:
<activity
android:name=".chat.app.activity.ChatPageActivity"
android:configChanges="orientation|screenLayout"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
答案 4 :(得分:0)
试试这个。它会将您的编辑文本滚动到焦点模式并为其打开键盘。
EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);