在任何的EditText点击屏幕时滚动起来 [英] screen scrolling up when clicking on any EditText

查看:170
本文介绍了在任何的EditText点击屏幕时滚动起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个屏,带4的EditText像MENUNAME,DishName,标签和价格。结果
我需要把我的标签EDITTEXT在顶部(+ 56dip)上点击就可以了。现在,它的正常工作。

I have one screen with 4 EditText like menuName, DishName,Tag and Price.
I need to set my tag Edittext at top(+56dip) on clicking on it. Right now it's working fine.

当我在标签点击的EditText那么它的转向像我想要什么。

When i click on Tag EditText then it's shift up like what i want.

我在哪里卡住

移布局,最多就是做工精细,但如果我在其他的EditText则点击它也将转移了,我不想要的。我只是wan't滚动屏幕,当我在标签的EditText点击。

Shifting layout up is working fine but if i click on other EditText then it will also shifting up which i don't want. I just wan't scroll Screen up when i click on Tag EditText.

下面是我的Java code。

Here is my java code.

    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文件code理解。

xml file code for understanding.

<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&apos;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" />

我努力,但din't得到任何东西。请给我任何参考或暗示。

I try hard but din't get anything. Please give me any reference or hint.

推荐答案

您code只对 heightDiff 为100执行之前<$ C $检查C> performScrollingUp()键,您的评论表明,当一个键盘弹出,将可能发生的。那么,当你点击任何EDITTEXT领域的键盘就会弹出...

Your code only checks for the heightDiff being over 100 before you execute performScrollingUp()and your comment indicates that would probably occur when a keyboard pops up. Well when you click on any editText field a keyboard would pop up...

解决方法:

您可以尝试code的首块更改为此:

Try changing your first block of code to this:

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是一个点击,而不是其他人。现在你只是聆听你的听众一个总体布局的变化。让我知道,如果这个工程。

This will make sure that it only scrolls up when the tagEditText was the one clicked and not the others. Right now you are just listening for an overall layout change with your listener. Let me know if this works.

这篇关于在任何的EditText点击屏幕时滚动起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆