Android 当显示软键盘时,向上平移视图的好方法是什么
我有一个设置了“adjustPan”选项的活动,当EditText接收到焦点并显示软键盘时,它会将我的视图向上平移,但我的问题是,在我的EditText下面有一些UI元素,当显示键盘时,它们也需要可见,而不是在较小的屏幕上。我可以告诉视图如何始终平移,以便通过将这些UI元素链接到EditText或其他内容使其可见,或者我必须手动控制平移并计算高度和全部,或者这里的好解决方案是什么?使用Android manifestfile中的stateHidden,您需要哪个类 android:WindowsOfInputMode=“stateHidden” 否则, 在活动中, getWindow().setSoftInputMode(Android 当显示软键盘时,向上平移视图的好方法是什么,android,android-layout,Android,Android Layout,我有一个设置了“adjustPan”选项的活动,当EditText接收到焦点并显示软键盘时,它会将我的视图向上平移,但我的问题是,在我的EditText下面有一些UI元素,当显示键盘时,它们也需要可见,而不是在较小的屏幕上。我可以告诉视图如何始终平移,以便通过将这些UI元素链接到EditText或其他内容使其可见,或者我必须手动控制平移并计算高度和全部,或者这里的好解决方案是什么?使用Android manifestfile中的stateHidden,您需要哪个类 android:Windows
WindowManager.LayoutParams.SOFT\u输入\u状态\u隐藏) 使用Android manifestfile中隐藏的stateHidden来选择所需的类 android:WindowsOfInputMode=“stateHidden” 否则, 在活动中, getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT\u输入\u状态\u隐藏) 您可以使用ViewTreeObserver进一步平移内容。 这是我的应用程序
int edtHeight = 0;
// llContent is the Viewgroup layout which is inside a ScrollView.
ViewTreeObserver viewTreeObserver1 = llContent.getViewTreeObserver();
viewTreeObserver1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
// Here edtHeight will get the height of edittext as i wanted my view to scroll that much bit more
edtHeight = edtTemprature.getHeight();
ViewTreeObserver obs = llContent.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
//llMain is the parent View Group of my XML layout
llMain.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
llMain.getWindowVisibleDisplayFrame(r);
int heightDiff = llMain.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100)
{ // if more than 100 pixels, its probably a keyboard...
if(edtTemprature.hasFocus()){
llContent.scrollTo(0, (int) (edtHeight * 1.5));
}else{
llContent.scrollTo(0, 0);
}
}
else
{
llContent.scrollTo(0, 0);
}
}
});
您可以使用ViewTreeObserver进一步平移内容。 这是我的应用程序
int edtHeight = 0;
// llContent is the Viewgroup layout which is inside a ScrollView.
ViewTreeObserver viewTreeObserver1 = llContent.getViewTreeObserver();
viewTreeObserver1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
// Here edtHeight will get the height of edittext as i wanted my view to scroll that much bit more
edtHeight = edtTemprature.getHeight();
ViewTreeObserver obs = llContent.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
//llMain is the parent View Group of my XML layout
llMain.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
llMain.getWindowVisibleDisplayFrame(r);
int heightDiff = llMain.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100)
{ // if more than 100 pixels, its probably a keyboard...
if(edtTemprature.hasFocus()){
llContent.scrollTo(0, (int) (edtHeight * 1.5));
}else{
llContent.scrollTo(0, 0);
}
}
else
{
llContent.scrollTo(0, 0);
}
}
});
不,我确实想要键盘,但我想在我的程序中控制它平移我的视图的距离/长度,我使用stateHidden和android:WindowsofInputMode=“adjustPan”来填充编辑文本。它工作正常..这完全是错误的,stateHidden指示活动不应显示软键盘。默认设置(您必须手动设置焦点以显示它),它对平移长度或其他任何内容都不起作用-这是错误的答案。否,我确实想要键盘,但我想在我的程序中控制它平移我的视图的距离/长度,我使用stateHidden和android:WindowsofInputMode=“adjustPan”来填充编辑文本。它工作正常..这完全是错误的,stateHidden指示活动不应显示软键盘。默认设置(您必须手动设置焦点以显示它),它对平移长度或其他任何事情都不起作用-这是一个错误的答案。是的,这也是我唯一的想法,我希望有一个更聪明的方法我也试图找到更好的方法,但最终使用了这个@Neigaard是的,这也是我唯一的想法,我希望有一个更聪明的方法我也试图找到更好的方法,但最终使用了这个@Neigaard