使顶部和Android的软键盘的设备之间的编辑文本出现 [英] make an edit text appear between the top and the soft keyboard of the device in android

查看:148
本文介绍了使顶部和Android的软键盘的设备之间的编辑文本出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多ViewsStubs滚动型布局,这些意见存根充气(显示)取决于用户的行为像.....回答问题1,使第二问题和答案字段(EDITTEXT)出现

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.

我想是任何新显示EDITTEXT /微调/复选框必须出现在屏幕上(动态滚动到这些意见)的顶部。

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

如何实现这一目标?

推荐答案

把我的code为例适合你,你可以这样做如下:
制作的 XML 为:

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>

然后code:

[1]获取设备的屏幕分辨率:高度,宽度

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

[2]把你的EditText的onFocusChange

[2] Take onFocusChange of your EditText

[3],如果您获得的EditText然后集中

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

[4]把那EditText上的底部,如果底部是大于(ScreenHeight / 3),那么

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

[5]滚动Your_ScrollView至(左,(ScreenHeight / 3));

[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

    }
}

这篇关于使顶部和Android的软键盘的设备之间的编辑文本出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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