2

多くの ViewsStubs を含むスクロールビュー レイアウトがあり、これらのビュー スタブは、次のようなユーザー アクションに応じて膨張 (表示) されます。最初の質問に答えると、2 番目の質問と回答フィールド (editText) が表示されます。

私の問題は、最初の質問に答えて送信をクリックした後、次の質問が表示されますが、回答フィールドはソフトキーボードの下に隠されています...ユーザーは自分が入力しているものを見ることができません。

私が望むのは、新しく表示された editText/spinner/checkbox が画面の上部に表示される必要があることです (これらのビューに動的にスクロールします)。

これを達成する方法??

4

4 に答える 4

1

私のコードを例として取り上げます。次のようなことができます。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>

そして、コード:

[1]デバイスの画面解像度を取得します:高さ、幅

[2]EditTextのonFocusChangeを取得します

[3]その中で、EditTextがフォーカスを取得すると、

[4]そのEditTextのBottomを取得します。そのBottomが(ScreenHeight / 3)より大きい場合は、

[5] Your_ScrollViewを(左、(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

    }
}
于 2012-07-18T15:28:38.697 に答える
0

次のレイアウトを使用すると、ソフト キーボードが表示されたときに下部の EditText が上に移動します (ある時点で view.setVisibility(true) を呼び出します)。

<?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>
于 2012-07-18T14:21:35.000 に答える
0

レイアウトでは、次のことを試すことができます。

1) 属性 android:fillViewPort="true" を設定します

2) 編集テキストの重みを「1」に設定します (他のオブジェクトの重みが 1 でない場合)

3) スクロールビューはすべてのテキストビュー/編集テキストを網羅していますか? そのfillViewPort属性でスクロールビュー内のレイアウト全体を囲むことを試みる価値があるかもしれません。

これは、スクロールビューとその複雑さのためのかなり良いリンクです:

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

于 2012-07-18T14:15:52.500 に答える
0

requestRectangleOnScreen メソッドを試して、ビューが表示されていることを確認できます。参照: 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. 
于 2012-07-18T13:00:58.823 に答える