package dw.haiziwang.com.myapplication; import android.app.Activity; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewTreeObserver; import android.widget.EditText; import android.widget.ScrollView; public class LoginActivity extends Activity { ScrollView scrollView; EditText editTextName; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); scrollView = (ScrollView)findViewById(R.id.scrollView); editTextName = (EditText)findViewById(R.id.id_input_name); listenerSoftInput(); } private void listenerSoftInput() { final View activityRootView = findViewById(R.id.scrollView); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); if (heightDiff > 100) { // 如果高度差超过100像素,就很有可能是有软键盘... scrollToBottom(); } else { } } }); } //scrollview滑到底部 protected void scrollToBottom() { handler.postDelayed(new Runnable() { @Override public void run() { scrollView.scrollTo(0,200); } }, 100); } }<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="dw.haiziwang.com.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
安卓登录界面让输入框滚动到指定位置的方法
最新推荐文章于 2022-01-29 15:06:13 发布