我有一个带有4个EditText的屏幕,如menuName,DishName,Tag和Price 我需要在点击它时将标签Edittext设置在顶部(+ 56dip)。现在它工作正常。
当我点击Tag EditText时,它会像我想要的那样向上移动。
我坚持的地方
移动布局工作正常但是如果我点击其他EditText那么它也会向上移动,我不想要。当我点击Tag EditText时,我只是想滚动屏幕。
这是我的java代码。
final View activityRootView = findViewById(R.id.RelativeAdd);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
Log.i("TEST",
"GLOBAL LAYOUT " + activityRootView.getHeight());
Log.i("TEST", "GLOBAL LAYOUT rel"+ relativeLayoutHeight);
if (heightDiff > 100) { // if more than 100 pixels, its
// probably a keyboard...
performScrollingUp();
Log.i("TEsT", "Keyboard visible");
} else {
Log.i("TEsT", "Keyboard not visible");
performScrollingDown();
txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
}
});
protected void performScrollingUp() {
if (!isTagUp) {
txtDishTags.setCursorVisible(true);
txtDishTags.setFocusable(true);
txtDishTags.setFocusableInTouchMode(true);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(txtDishTags, 0);
firstRelativeLayout.setVisibility(View.GONE);
secondRelativeLayout.setVisibility(View.GONE);
firstLinearLayout.setVisibility(View.GONE);
secondLinearLayout.setVisibility(View.GONE);
rlp = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
android.app.ActionBar.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, R.id.tempRelativeLayout);
tagRelativeLayout.setLayoutParams(rlp);
isTagUp = true;
final float scale = getResources().getDisplayMetrics().density;
int width = (int) (60 * scale + 0.5f);
int height = (int) (44 * scale + 0.5f);
rlp2 = new RelativeLayout.LayoutParams(width, height);
rlp2.addRule(RelativeLayout.BELOW, R.id.tempRelativeLayout);
rlp2.setMargins(24, 0, 0, 0);
thirdRelativLayout.setLayoutParams(rlp2);
lbltag.setGravity(Gravity.CENTER);
}
}
用于理解的xml文件代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeAdd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF"
android:orientation="vertical">
<ViewStub
android:id="@+id/vsHeader2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/header"
android:layout="@layout/copyofheader" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/myRelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dip"
android:orientation="vertical" >
<EditText
android:id="@+id/hotelName"
android:layout_width="fill_parent"
android:layout_height="44dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:background="@drawable/borderforloginedittext"
android:ems="10"
android:hint="Name on menu (optional)"
android:inputType="textCapWords"
android:paddingLeft="70dp"
android:textColor="#333333"
android:textColorHint="#999999"
android:textSize="16sp" >
<requestFocus />
</EditText>
</LinearLayout>
<RelativeLayout
android:id="@+id/firstRelativeLayout"
android:layout_width="60dip"
android:layout_height="44dip"
android:layout_marginLeft="12dip"
android:layout_marginTop="12dip" >
<TextView
android:id="@+id/labelName"
android:layout_width="60dip"
android:layout_height="44dip"
android:gravity="center"
android:text="NAME"
android:textColorHint="#999999"
android:textSize="10sp"
android:visibility="visible" >
</TextView>
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/firstRelativeLayout"
android:layout_marginTop="-2dip"
android:orientation="vertical" >
<EditText
android:id="@+id/dishName"
android:layout_width="fill_parent"
android:layout_height="44dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:background="@drawable/borderforloginedittext"
android:ems="10"
android:hint="(require)"
android:inputType="textCapWords|textPersonName"
android:paddingLeft="70dp"
android:textColor="#333333"
android:textColorHint="#999999"
android:textSize="16sp" >
</EditText>
</LinearLayout>
<RelativeLayout
android:id="@+id/secondRelativeLayout"
android:layout_width="60dip"
android:layout_height="44dip"
android:layout_below="@+id/firstRelativeLayout"
android:layout_marginLeft="12dip"
android:layout_marginTop="-2dip" >
<TextView
android:id="@+id/labelDish"
android:layout_width="60dip"
android:layout_height="44dip"
android:gravity="center"
android:text="DISH"
android:textColorHint="#999999"
android:textSize="10sp"
android:visibility="visible" >
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/secondRelativeLayout"
android:layout_marginTop="-2dip"
>
<AutoCompleteTextView
android:id="@+id/txtTags"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="44dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:background="@drawable/borderforloginedittext"
android:hint="eg: Breakfast, spicy, italian "
android:inputType="textNoSuggestions|textMultiLine"
android:imeOptions="actionNext"
android:paddingLeft="70dip"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:dropDownHeight="fill_parent"
android:dropDownWidth="match_parent"
android:textColor="#333333"
android:singleLine="false"
android:textColorHint="#999999"
android:textSize="16sp" >
</AutoCompleteTextView>
<RelativeLayout
android:id="@+id/progressLayout"
android:layout_width="23dip"
android:layout_height="23dip"
android:layout_alignRight="@+id/txtTags"
android:layout_centerVertical="true"
android:layout_marginRight="4dip"
android:layout_marginTop="10dip"
android:layout_below="@+id/linearLayout2"
android:visibility="gone" >
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/thirdRelativeLayout"
android:layout_width="60dip"
android:layout_height="44dip"
android:layout_below="@+id/secondRelativeLayout"
android:layout_marginLeft="12dip"
android:layout_marginTop="-2dip" >
<TextView
android:id="@+id/labeltags"
android:layout_width="60dip"
android:layout_height="44dip"
android:gravity="center"
android:text="TAGS"
android:textColorHint="#999999"
android:textSize="10sp"
android:visibility="visible" >
</TextView>
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout3"
android:layout_marginTop="-2dip"
android:orientation="vertical" >
<EditText
android:id="@+id/menuPrice"
android:layout_width="fill_parent"
android:layout_height="44dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:background="@drawable/borderforloginedittext"
android:ems="10"
android:hint="(optional) "
android:inputType="number"
android:paddingLeft="70dp"
android:textColor="#333333"
android:textColorHint="#999999"
android:textSize="16sp" >
</EditText>
</LinearLayout>
<RelativeLayout
android:id="@+id/fourthRelativeLayout"
android:layout_width="60dip"
android:layout_height="44dip"
android:layout_below="@+id/linearLayout3"
android:layout_marginLeft="12dip"
android:layout_marginTop="-2dip" >
<TextView
android:id="@+id/labelprice"
android:layout_width="60dip"
android:layout_height="44dip"
android:gravity="center"
android:text="PRICE"
android:textColorHint="#999999"
android:textSize="10sp"
android:visibility="visible" >
</TextView>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fourthRelativeLayout"
android:layout_marginTop="5dip" >
<Button
android:id="@+id/btnAddSecondaryHotel"
android:layout_width="match_parent"
android:layout_height="44dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:layout_marginTop="12dip"
android:layout_weight="0.5"
android:background="@color/disabledButtonColor"
android:text="Let's go rate this dish!"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="57dip"
android:layout_height="57dip"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dip"
android:layout_marginTop="100dip"
android:src="@drawable/dishoomit" />
我努力但却没有得到任何东西。请给我任何参考或提示。
答案 0 :(得分:2)
在可以解决此问题的AndroidManifest.xml中
<activity
android:name="com.example.mainActivity"
android:windowSoftInputMode="adjustPan">
</activity>
答案 1 :(得分:2)
您的代码仅在执行heightDiff
之前检查performScrollingUp()
超过100,并且您的注释表明当键盘弹出时可能会发生这种情况。那么当你点击任何editText字段时,会弹出一个键盘......
修复:
尝试将您的第一个代码块更改为:
final View activityRootView = findViewById(R.id.RelativeAdd);
EditText tagEditText = findViewById(R.id.txtTags);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
Log.i("TEST",
"GLOBAL LAYOUT " + activityRootView.getHeight());
Log.i("TEST", "GLOBAL LAYOUT rel"+ relativeLayoutHeight);
if (heightDiff > 100 && tagEditText.isFocused()) { // if more than 100 pixels, its
// probably a keyboard...
performScrollingUp();
Log.i("TEsT", "Keyboard visible");
} else {
Log.i("TEsT", "Keyboard not visible");
performScrollingDown();
txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
}
});
这将确保它只在tagEditText是单击的而不是其他的时向上滚动。现在,您只是在听取整个布局的变化。如果有效,请告诉我。
答案 2 :(得分:1)
我不确定这是否有效。但我至少会这样做:
if (heightDiff > 100) { // if more than 100 pixels, its
// probably a keyboard...
if(!isTagUp)
performScrollingUp();
Log.i("TEsT", "Keyboard visible");
isTagUp = true; // Keyboard is up
} else {
Log.i("TEsT", "Keyboard not visible");
if(isTagUp)
{
performScrollingDown();
txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
isTagUp = false; // Keyboard is down again
}
这样甚至不会调用该函数。可能不会解决它,但试一试..