屏幕底部有一个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滚动到按钮正上方Manifest:
<?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) {
}
});
}
}
答案 0 :(得分:1)
将paddingBottom用于ScrollView(LinearLayout)的父级。 如果Button height = 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>