Android:当键盘出现时,如何滚动滚动视图以显示下一个字段
我使用scrollview并设置WindowsOfInputMode=SoftInput.AdjustResize,但当键盘出现时,我的scrollview不会滚动 当键盘出现时隐藏我的编辑文本。 下面是我的scrollview代码Android:当键盘出现时,如何滚动滚动视图以显示下一个字段,android,android-edittext,keyboard,scrollview,android-softkeyboard,Android,Android Edittext,Keyboard,Scrollview,Android Softkeyboard,我使用scrollview并设置WindowsOfInputMode=SoftInput.AdjustResize,但当键盘出现时,我的scrollview不会滚动 当键盘出现时隐藏我的编辑文本。 下面是我的scrollview代码 <ScrollView android:id="@+id/scrollView" android:clipToPadding="false" android:layout_width="mat
<ScrollView
android:id="@+id/scrollView"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:isScrollContainer="true"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:paddingLeft="@dimen/padding_30"
android:paddingRight="@dimen/padding_30"
android:paddingBottom="@dimen/padding_30"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/TopWelcomeLayout"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/signupImg"
android:layout_marginTop="25dp"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:src="@drawable/splitssignuplogo" />
<TextView
android:layout_width="match_parent"
android:id="@+id/welcomeText"
android:layout_below="@id/signupImg"
android:layout_marginTop="22dp"
android:layout_height="wrap_content"
android:textColor="@color/colorGray"
android:lines="3"
android:textSize="@dimen/textSize_16"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="When it comes to owning your songs importan"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_below="@id/TopWelcomeLayout"
android:id="@+id/avtarLayout"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center">
<refractored.controls.CircleImageView
android:id="@+id/avtar"
android:layout_height="110dp"
android:layout_width="110dp"
android:transitionName="EXTRA_IMAGE"
android:src="@drawable/signupuser"/>
<ImageView
android:id="@+id/cameraIcon"
android:layout_height="40dp"
android:layout_width="40dp"
android:src="@drawable/CameraSignUp"
android:tint="#9195aa"
android:layout_marginLeft="35dp"
android:layout_marginTop="35dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/emailUpdateLayout"
android:layout_below="@id/avtarLayout"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:id="@+id/emailTextForUpdateAccount"
android:layout_marginTop="13dp"
android:layout_marginBottom="40dp"
android:text="dgehdg@gmail.com"
android:textColor="#9195AA"
android:layout_below="@+id/avtar"
android:textSize="@dimen/textSize_14"
android:layout_height="wrap_content"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/emailUpdateLayout"
android:id="@+id/nameLayout"
android:layout_marginTop="@dimen/margin_17"
android:weightSum="100">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginRight="@dimen/margin_10"
android:layout_height="wrap_content"
android:id="@+id/FirstNameWrapper"
app:errorTextAppearance="@style/ErrorText"
android:layout_marginTop="@dimen/margin_10"
android:theme="@style/CommonTextStyleTheme"
android:layout_weight="50">
<EditText
android:id="@+id/efirstname"
android:nextFocusDown="@+id/elastname"
android:textColor="@color/colorLightBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:textSize="@dimen/textSize_14"
android:paddingBottom="@dimen/padding_20"
android:hint="FIRST NAME *" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
app:errorTextAppearance="@style/ErrorText"
android:id="@+id/LastNameWrapper"
android:theme="@style/CommonTextStyleTheme"
android:layout_marginLeft="@dimen/margin_10"
android:layout_marginTop="@dimen/margin_10">
<EditText
android:id="@+id/elastname"
android:nextFocusDown="@+id/eemail"
android:textColor="@color/colorLightBlue"
android:textSize="@dimen/textSize_14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:paddingBottom="@dimen/padding_20"
android:hint="LAST NAME *" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameLayout"
app:errorTextAppearance="@style/ErrorText"
android:id="@+id/emailLayout"
android:theme="@style/CommonTextStyleTheme"
android:layout_marginTop="@dimen/margin_15">
<EditText
android:id="@+id/eemail"
android:nextFocusDown="@+id/epassword"
android:textColor="@color/colorLightBlue"
android:layout_width="match_parent"
android:textSize="@dimen/textSize_14"
android:paddingBottom="@dimen/padding_20"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="EMAIL *" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/emailLayout"
app:errorTextAppearance="@style/ErrorText"
android:id="@+id/confirmemailLayout"
android:theme="@style/CommonTextStyleTheme"
android:layout_marginTop="@dimen/margin_15">
<EditText
android:id="@+id/econfirmemail"
android:nextFocusDown="@+id/epassword"
android:textColor="@color/colorLightBlue"
android:layout_width="match_parent"
android:textSize="@dimen/textSize_14"
android:paddingBottom="@dimen/padding_20"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="CONFIRM EMAIL *" />
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phoneLayout"
android:layout_below="@id/confirmemailLayout"
android:layout_marginTop="@dimen/margin_15"
android:paddingBottom="5sp">
<TextView
android:id="@+id/lblphonenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textColor="#9195aa"
app:fontFamily="@font/lato_regular"
android:textSize="@dimen/textSize_12"
android:text="PHONE NUMBER *" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_centerVertical="true"
android:layout_below="@+id/lblphonenumber"
android:id="@+id/CountryDropLayout">
<TextView
android:id="@+id/countrySpinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingRight="15dp"
android:textColor="#4C5375"
android:layout_centerVertical="true"
android:textSize="@dimen/textSize_14"
android:gravity="center_vertical"
android:maxLines="1"
android:text="US"
android:singleLine="true"
android:background="@null"
android:textCursorDrawable="@null"
android:inputType="textPhonetic" />
<ImageButton
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/downarrow"
android:background="@null"
android:layout_toRightOf="@+id/countrySpinner"
android:id="@+id/btnDropDown"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/lblcountrycode"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/margin_15"
android:layout_toRightOf="@+id/countrySpinner"
android:layout_marginLeft="8dp"
android:paddingTop="11sp"
android:textColor="#9195aa"
android:textSize="@dimen/textSize_14"
android:text="+1" />
</RelativeLayout>
<EditText
android:id="@+id/ephone"
android:layout_width="match_parent"
android:nextFocusDown="@+id/einstagram"
android:textSize="@dimen/textSize_14"
android:layout_height="40dp"
android:background="@null"
android:paddingLeft="@dimen/margin_10"
android:gravity="center_vertical"
android:layout_below="@+id/lblphonenumber"
android:layout_toRightOf="@+id/CountryDropLayout"
android:textColor="@color/colorLightBlue"
android:inputType="number"
android:hint="PHONE NUMBER *" />
<TextView
android:id="@+id/lbldivider"
android:layout_width="match_parent"
android:layout_marginLeft="3dp"
android:layout_marginTop="2sp"
android:layout_below="@+id/CountryDropLayout"
android:layout_height="1dp"
android:background="#efeff2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number is required."
android:id="@+id/textViewError"
android:layout_below="@+id/lbldivider"
android:textSize="10sp"
android:paddingTop="5dp"
android:visibility="gone"
android:textColor="#f81353"
/>
</RelativeLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/phoneLayout"
android:id="@+id/passwordLayout"
app:errorTextAppearance="@style/ErrorText"
android:theme="@style/CommonTextStyleTheme"
android:layout_marginTop="@dimen/margin_15">
<EditText
android:id="@+id/epassword"
android:imeOptions="actionDone"
android:textColor="@color/colorLightBlue"
android:layout_width="match_parent"
android:textSize="@dimen/textSize_14"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:paddingBottom="@dimen/padding_20"
android:hint="CREATE PASSWORD *" />
</android.support.design.widget.TextInputLayout>
<TextView
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_below="@id/passwordLayout"
android:layout_marginTop="@dimen/margin_30"
android:id="@+id/errorText"
android:textColor="#f81353"
android:text="swgdsfwgdfwfdgwfdfdf"
android:textSize="@dimen/textSize_13"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="@id/errorText"
android:id="@+id/btnLayout"
android:layout_height="wrap_content">
<Button
android:text="SIGN UP"
android:visibility="visible"
android:alpha="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_greenbutton"
android:layout_marginTop="@dimen/margin_20"
android:textColor="@color/colorWhite"
android:padding="@dimen/padding_20"
android:textSize="@dimen/textSize_16"
android:textStyle="bold"
android:id="@+id/btnSignUp" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
在我的设备中,它工作正常,我没有在清单中添加任何内容,也没有添加这一个WindowsOfInputMode=SoftInput.AdjustResize
我在主布局中只有android:fitsystemwindows=“true”
您可以通过使用setOnFocusChangeListener函数对视图对象调用OnFocusChange()来实现类似的功能
sView = findViewById(R.id.scrollView);
email = findViewById(R.id.eemail);
fname = findViewById(R.id.efirstname);
fname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
boolean b = sView.postDelayed(new Runnable() {
@Override
public void run() {
sView.smoothScrollBy(0,300);
}
},800) ;
}
});
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
boolean b = sView.postDelayed(new Runnable() {
@Override
public void run() {
sView.smoothScrollBy(0,300);
}
},800) ;
}
});
我注意到的另一件事是,您正在将编辑文本与文本输入布局一起使用。您可以使用文本输入文本Ankit,因为它有部分答案。他回答的问题是,即使你的视图当前在屏幕上可见,它也会被滚动。有了这段代码,您将摆脱错误滚动视图的糟糕用户体验
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ScrollView;
public class MainActivity extends AppCompatActivity {
private ScrollView sView;
private int heightDiff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sView = findViewById(R.id.scrollView);
//Here we get the height of soft keyboard by observing changes of the scrollView's height.
sView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
heightDiff = sView.getRootView().getHeight() - sView.getHeight();
}
});
final EditText email = findViewById(R.id.eemail);
EditText firstName = findViewById(R.id.efirstname);
firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!isVisibleWhileSoftKeyboardShowing(email) && hasFocus) {
sView.postDelayed(new Runnable() {
@Override
public void run() {
sView.smoothScrollBy(0, 200);
}
}, 500);
}
}
});
}
/**
* check if a view is currently visible in the screen or not
*
* @param view
* @return
*/
public boolean isVisibleWhileSoftKeyboardShowing(final View view) {
if (view == null) {
return false;
}
if (!view.isShown()) {
return false;
}
final Rect actualPosition = new Rect();
view.getGlobalVisibleRect(actualPosition);
final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight() - heightDiff);
return actualPosition.intersect(screen);
}
/**
* to get screen width
*
* @return
*/
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
/**
* to get screen height
*
* @return
*/
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
}
我希望这正是您要找的东西。您不能增加软键盘和编辑文本之间的距离,但您可以通过手动滚动页面来实现,这是一种肮脏的方式
efirstname.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Handler(Looper.getMainLooper()).postDelayed({
scrollView.scrollBy(0, 200)
}, 100)
}
});
}
您需要在ScrollView中添加以下几行:
android:paddingBottom="200dp"
android:clipToPadding="false"
在Android清单中,输入:
<activity
android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />
试试这个,因为这对我来说也很有效,祝你好运。我已经尝试过了,但不起作用。我正在查找解决方案上面的图像显示我的问题可能重复的请帮助我解决此问题这不是我的问题,我的问题是如果单击名字,然后单击列表电子邮件编辑文本显示。在这种情况下,如果您选择电子邮件编辑文本来填写,您的焦点将更改为电子邮件。这只是一辆轻便马车的设计。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">