键盘出现时隐藏对齐按钮

时间:2015-09-16 10:16:35

标签: android keyboard

美好的一天,我有一个带有edittext和按钮的表单,它与底部对齐。因此,当我尝试在编辑文本中键入内容时,键盘会出现,而我的按钮也会在键盘上方移动。我读了一些主题,我应该在我的Manifest文件中使用android:windowSoftInputMode="adjustPan",它解决了问题,但也创建了一个新的:当我隐藏键盘我的屏幕跳转 - 工具栏移动状态栏然后转到以前的状态。 这是我的表单xml布局:

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

<include
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="17dp"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="17dp"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="16sp" />

            <EditText
                android:id="@+id/nameTextField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:hint="Имя" />

            <EditText
                android:id="@+id/surnameTextField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:hint="Фамилия" />

            <EditText
                android:id="@+id/phoneTestField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:hint="Номер телефона"
                android:phoneNumber="true" />

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/sendButton"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:background="@color/myOrange"
        android:text="Отправить заявку!"
        android:textAllCaps="false"
        android:textColor="#ffffffff"
        android:textSize="20sp" />
</RelativeLayout>

当用户触摸除当前edittext小部件之外的任何区域时,我也会隐藏键盘:

public class ServicesFormActivity extends AppCompatActivity {

public final static String RIGHTS = "Оформление прав";
public final static String AUTO = "Покупка машиномест";
private String form_name;
private Context context;
private static EditText nameTextField;
private static EditText phoneTestField;
private static EditText surnameTextField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_services_form);
    context = this;
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    form_name = (String) getIntent().getExtras().getSerializable("form_name");
    setTitle(form_name);
    final ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
    nameTextField = (EditText) findViewById(R.id.nameTextField);
    phoneTestField = (EditText) findViewById(R.id.phoneTestField);
    surnameTextField = (EditText) findViewById(R.id.surnameTextField);
    nameTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                hideKeyboard(v);
            }
        }
    });
    phoneTestField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                hideKeyboard(v);
            }
        }
    });
    surnameTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                hideKeyboard(v);
            }
        }
    });
}



public void hideKeyboard(View view) {
    InputMethodManager inputMethodManager =(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}

我还在我的清单中设置了android:windowSoftInputMode="adjustPan"。那么问题是什么,也就是为什么视图会跳跃?也许我应该让我的状态栏和工具栏固定,这样他们就不动了? THX

0 个答案:

没有答案