在android中,在设备的顶部和软键盘之间显示编辑文本

在android中,在设备的顶部和软键盘之间显示编辑文本,android,focus,scrollview,Android,Focus,Scrollview,我有一个带有许多ViewsStub的scrollview布局,这些视图Stub会根据用户的操作而膨胀(显示),例如……回答第一个问题会显示第二个问题和答案字段(editText) 我的问题是在回答第一个问题并单击“提交”后,下一个问题显示出来,但答案字段隐藏在软键盘下…用户无法看到他正在键入的内容 我想要的是任何新显示的editText/微调器/复选框必须出现在屏幕顶部(动态滚动到这些视图) 如何实现这一点???您可以尝试requestRectangleOnScreen方法以确保视图可见。 见:

我有一个带有许多ViewsStub的scrollview布局,这些视图Stub会根据用户的操作而膨胀(显示),例如……回答第一个问题会显示第二个问题和答案字段(editText)

我的问题是在回答第一个问题并单击“提交”后,下一个问题显示出来,但答案字段隐藏在软键盘下…用户无法看到他正在键入的内容

我想要的是任何新显示的editText/微调器/复选框必须出现在屏幕顶部(动态滚动到这些视图)


如何实现这一点???

您可以尝试requestRectangleOnScreen方法以确保视图可见。 见:


在布局中,您可以尝试以下操作:

1) 设置属性android:fillViewPort=“true”

2) 将编辑文本的权重设置为“1”(假设没有其他对象的权重为1)

3) 您的滚动视图是否包含所有文本视图/编辑文本?可能值得尝试在scrollview中使用该fillViewPort属性包围整个布局

对于ScrollView及其复杂性,这是一个非常好的链接:


当显示软键盘时,使用以下布局应将底部编辑文本向上移动(在某个点调用view.setVisibility(true)):


    
    

以我的代码为例,您可以执行以下操作: 将XML设置为:

<?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>

这是我尝试的第一件事…使视图最初不可见,然后动态地使它们可见…这里发生的是当用户调用此活动时,他将看到一个长的可滚动布局,只有第一个视图可见,这告诉他,这个活动有一个很长的形式,视图是不可见的……所以我开始使用视图存根,它在膨胀(或可见)之前不具有任何宽度和高度……所以我的问题是……即使使用视图存根,当我使用setVisibility()使视图动态可见时,它是否也具有相同的效果??
<?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>
<?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>
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

    }
}