当键盘出现时,如何使按钮不与 EditText 重叠?
How to make a button don't overlap the EditText when keyboard showing up?
屏幕底部有一个bottom_sheet_behavior按钮,当键盘出现时,它显示在键盘的顶部。我想保持这种状态。
问题是,当我选择要输入的 EditText 时,按钮显示在键盘顶部,它与 EditText 的一半重叠,如下所示:
所以我希望 EditText 自动向上滚动一点以完整显示自己。我试过 MainActivity.java onCreate() {
mEditText.scrollTo(0, mEditText.getTop());
尝试过
mEditText.scrollTo(0, mEditText.getTop() + 20);
// to try if it will scroll up more in this way. it didn't.
但是当键盘出现时,编辑文本仍然像以前一样完全重叠了一半。我想我以错误的方式调用了该方法?
软键盘不与EditText重叠,但底部按钮重叠。当键盘显示时,是否可以通过编程方式将 EditText 滚动到按钮的正上方?
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robyn.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"> // i tried make this "adjustPan" it behaves the same.
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.robyn.myapplication.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:text="Input:"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/save"
android:text="save"
android:backgroundTint="@color/colorAccent"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
// bottom button overlaps edittext
private EditText mEditText;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
mEditText = (EditText)findViewById(R.id.input);
mEditText.scrollTo(0, mEditText.getTop());
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
mButton = (Button)findViewById(R.id.save);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
使用 paddingBottom 作为 ScrollView (LinearLayout) 的父级。
如果按钮高度 = 50dp,将 paddingBottom="50dp" 设置为 LinearLayout。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingBottom="50dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:text="Input:"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/save"
android:text="save"
android:backgroundTint="@color/colorAccent"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>
屏幕底部有一个bottom_sheet_behavior按钮,当键盘出现时,它显示在键盘的顶部。我想保持这种状态。
问题是,当我选择要输入的 EditText 时,按钮显示在键盘顶部,它与 EditText 的一半重叠,如下所示:
mEditText.scrollTo(0, mEditText.getTop());
尝试过
mEditText.scrollTo(0, mEditText.getTop() + 20);
// to try if it will scroll up more in this way. it didn't.
但是当键盘出现时,编辑文本仍然像以前一样完全重叠了一半。我想我以错误的方式调用了该方法?
软键盘不与EditText重叠,但底部按钮重叠。当键盘显示时,是否可以通过编程方式将 EditText 滚动到按钮的正上方?
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robyn.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"> // i tried make this "adjustPan" it behaves the same.
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.robyn.myapplication.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:text="Input:"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/save"
android:text="save"
android:backgroundTint="@color/colorAccent"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
// bottom button overlaps edittext
private EditText mEditText;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
mEditText = (EditText)findViewById(R.id.input);
mEditText.scrollTo(0, mEditText.getTop());
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
mButton = (Button)findViewById(R.id.save);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
使用 paddingBottom 作为 ScrollView (LinearLayout) 的父级。 如果按钮高度 = 50dp,将 paddingBottom="50dp" 设置为 LinearLayout。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingBottom="50dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:text="Input:"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/save"
android:text="save"
android:backgroundTint="@color/colorAccent"
android:layout_alignParentBottom="true"
android:textColor="#FFFFFF"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>