make an edit text appear between the top and the s

2019-07-11 04:38发布

问题:

I have a scrollview layout with many ViewsStubs and these views stubs are inflated (displayed) depending on the user actions like.....answering the 1st questions makes the 2nd question and answer field(editText) appear

my problem is after answering the 1st question and clicking submit, the next question is made visible but the answer field is hidden under the soft keyboard...user cannot see what he is typing.

what i want is any newly displayed editText/spinner/checkbox must appear at the top of the screen (dynamically scroll to these views).

how to achieve this??

回答1:

Take my code as example for you, You can do something like following: Make XML as:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrlv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#123789" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#1188ff" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/headerlayout"
                android:background="#456789" >

                <EditText
                    android:id="@+id/etemail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="40dp"
                    android:hint="Text 1"
                    android:maxLines="3" />

                <EditText
                    android:id="@+id/etpassword"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/etemail"
                    android:layout_alignRight="@+id/etemail"
                    android:layout_below="@+id/etemail"
                    android:hint="Text 2"
                    android:inputType="textPassword"
                    android:maxLines="3" />

                <EditText
                    android:id="@+id/etusername"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/etemail"
                    android:layout_alignRight="@+id/etemail"
                    android:layout_below="@+id/etpassword"
                    android:hint="Text 3"
                    android:maxLines="3" />

                <EditText
                    android:id="@+id/etphone"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/etemail"
                    android:layout_alignRight="@+id/etemail"
                    android:layout_below="@+id/etusername"
                    android:hint="Text 4"
                    android:inputType="text"
                    android:maxLines="5" />

                <CheckBox
                    android:id="@+id/chkterms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/etemail"
                    android:layout_alignRight="@+id/etemail"
                    android:layout_below="@+id/etphone"
                    android:layout_marginTop="10dp"
                    android:text="Conditions Aplly"
                    android:textColor="@android:color/black" />

                <Button
                    android:id="@+id/btnlogin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/chkterms"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:text="Login" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

And then code:

[1] Get device's screen resolution: Height, Width

[2] Take onFocusChange of your EditText

[3] In that, if your EditText gets focus then

[4] Get that EditText's Bottom, if that Bottom is greater then (ScreenHeight / 3) then

[5] Scroll Your_ScrollView to (left, (ScreenHeight/3));

public class MainActivity extends Activity implements OnClickListener{
    private EditText etEmail, etPassword, etUserName, etPhoneNo;
    private CheckBox chkTerms;
    private Button btnLogin;
    ScrollView scrlv;

    int sw, sh, left, bottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrlv = (ScrollView) findViewById(R.id.scrlv);

        etEmail = (EditText) findViewById(R.id.etemail);
        etPassword = (EditText) findViewById(R.id.etpassword);
        etUserName = (EditText) findViewById(R.id.etusername);
        etPhoneNo = (EditText) findViewById(R.id.etphone);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        sw = dm.widthPixels;
        sh = dm.heightPixels;
        System.out.println("Screen Width = " + sw);
        System.out.println("Screen Height = " + sh);

        chkTerms = (CheckBox) findViewById(R.id.chkterms);

        btnLogin = (Button) findViewById(R.id.btnlogin);
        btnLogin.setOnClickListener(this);

        etUserName.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(etUserName.hasFocus()) {
                    left = etUserName.getLeft();
                    bottom = etUserName.getTop();
                    if (bottom > sh / 3) {
                        System.out.println("Umn Left :: " + left);
                        System.out.println("Umn Bottom :: " + bottom);
                        scrlv.scrollTo(left, (sh/3));
                        left = etUserName.getLeft();
                        bottom = etUserName.getTop();
                        System.out.println("Umn Left :: " + left);
                        System.out.println("Unm Bottom :: " + bottom);
                    }
                }
            }
        });

        etPhoneNo.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(etPhoneNo.hasFocus()) {
                    left = etPhoneNo.getLeft();
                    bottom = etPhoneNo.getTop();
                    if (bottom > sh / 3) {
                        System.out.println("Phno Left :: " + left);
                        System.out.println("Phno Bottom :: " + bottom);
                        scrlv.scrollTo(0, (sh/3));
                        left = etPhoneNo.getLeft();
                        bottom = etPhoneNo.getTop();
                        System.out.println("Phno Left :: " + left);
                        System.out.println("Phno Bottom :: " + bottom);
                    }
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}


回答2:

In your layout, you can try some of the following things:

1) set the attribute android:fillViewPort="true"

2) set the weight of your edit text to "1" (assuming no other object has a weight of 1)

3) Is your scrollview encompassing all your textviews/edittexts? It might be worth trying to surround your entire layout within the scrollview with that fillViewPort attribute.

this is a pretty good link for scrollviews and their complexity:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/



回答3:

Using the following layout should move the bottom EditText up when the soft keyboard is displayed (call view.setVisibility(true) at some point):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/answers" />

    <EditText
        android:id="@+id/answers"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:visibility="invisible"/>

</RelativeLayout>


回答4:

You can try the requestRectangleOnScreen method to make sure your view is visible. See: http://developer.android.com/reference/android/view/View.html#requestRectangleOnScreen%28android.graphics.Rect%29

public boolean requestRectangleOnScreen (Rect rectangle)
Since: API Level 1

Request that a rectangle of this view be visible on the screen, scrolling if necessary just enough.

A View should call this if it maintains some notion of which part of its content is interesting. For example, a text editing view should call this when its cursor moves.
Parameters
rectangle   The rectangle.
Returns

    Whether any parent scrolled.