如何更改 EditText 的光标滚动行为?
How do I change the cursor scroll behavior for an EditText?
我刚刚做了一个超级基本的测试,我所做的就是使用 hello world 示例模板并添加一个多行文本对象。文本对象位于屏幕底部。当我输入多行文字,然后移动光标时,整个屏幕向下滚动,所以软键盘开始盖住文本框。我正在尝试(在一个更复杂的应用程序中但遇到同样的问题,因此使用基本测试用例)使行为与默认消息传递应用程序匹配:如果您移动光标,它不会平移整个视口,并且只会适当地滚动文本对象。视觉上更容易解释。
这里有 3 行,我要向上滚动(不管我使用箭头键还是使用小泪滴选择器)。
在这里,我抓住了小的选择器泪珠状物并向上移动了 2 行:请注意整个 window 已经向下滚动。我不想要那种行为。我正在寻找的是默认消息应用程序中的这个:
注意移动光标不会滚动视口。它根本不滚动。如果我有太多的文本无法容纳,它只会根据需要滚动编辑文本的内容,而不滚动其他任何内容。
我如何获得这种行为?以防万一,对于这个简单的测试,这里是布局的样子。
编辑:根据要求,我发布了 xml 和 java。请注意,这只是添加了编辑文本的默认 Android Studio hello world 项目。
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jeffw.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:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.jeffw.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.529"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.705" />
<EditText
android:id="@+id/editText"
android:layout_width="363dp"
android:layout_height="109dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:layout_editor_absoluteX="11dp" />
</android.support.constraint.ConstraintLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.example.jeffw.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
和 java 代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
试试这个:
// SCROLL VIEW HACK
// BOGUS
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
scrollView .setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
scrollView .setFocusable(false);
scrollView .setFocusableInTouchMode(false);
scrollView .setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch();
return false;
}
});
我觉得对你有帮助。
要实现此行为,您应该将此 android:windowSoftInputMode="adjustPan"
和 android:isScrollContainer="true"
添加到清单文件中的 activity。
有关更多详细信息,您可以查看开发人员文档。 https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
更新
将此更新到您的清单 android:windowSoftInputMode="adjustResize|stateHidden"
..我使用 RelativeLayout 而不是 android.support.constraint.ConstraintLayout
,它对我来说工作正常。
我刚刚做了一个超级基本的测试,我所做的就是使用 hello world 示例模板并添加一个多行文本对象。文本对象位于屏幕底部。当我输入多行文字,然后移动光标时,整个屏幕向下滚动,所以软键盘开始盖住文本框。我正在尝试(在一个更复杂的应用程序中但遇到同样的问题,因此使用基本测试用例)使行为与默认消息传递应用程序匹配:如果您移动光标,它不会平移整个视口,并且只会适当地滚动文本对象。视觉上更容易解释。
这里有 3 行,我要向上滚动(不管我使用箭头键还是使用小泪滴选择器)。
在这里,我抓住了小的选择器泪珠状物并向上移动了 2 行:请注意整个 window 已经向下滚动。我不想要那种行为。我正在寻找的是默认消息应用程序中的这个:
注意移动光标不会滚动视口。它根本不滚动。如果我有太多的文本无法容纳,它只会根据需要滚动编辑文本的内容,而不滚动其他任何内容。
我如何获得这种行为?以防万一,对于这个简单的测试,这里是布局的样子。
编辑:根据要求,我发布了 xml 和 java。请注意,这只是添加了编辑文本的默认 Android Studio hello world 项目。 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jeffw.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:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.jeffw.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.529"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.705" />
<EditText
android:id="@+id/editText"
android:layout_width="363dp"
android:layout_height="109dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:layout_editor_absoluteX="11dp" />
</android.support.constraint.ConstraintLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.example.jeffw.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
和 java 代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
试试这个:
// SCROLL VIEW HACK
// BOGUS
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
scrollView .setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
scrollView .setFocusable(false);
scrollView .setFocusableInTouchMode(false);
scrollView .setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch();
return false;
}
});
我觉得对你有帮助。
要实现此行为,您应该将此 android:windowSoftInputMode="adjustPan"
和 android:isScrollContainer="true"
添加到清单文件中的 activity。
有关更多详细信息,您可以查看开发人员文档。 https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
更新
将此更新到您的清单 android:windowSoftInputMode="adjustResize|stateHidden"
..我使用 RelativeLayout 而不是 android.support.constraint.ConstraintLayout
,它对我来说工作正常。