For example, in the operation of login, the user must want to directly click the login button after entering the password. Back key hide soft keyboard such experience must be very bad, programmers, encountered problems to solve the problem.




To achieve 1

xml

  

      

          

          

          

          

          

        Copy the code  

      
Copy the code

java

mScrollView = (ScrollView) view.findViewById(R.id.scrollview); usernamelogin_username.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { changeScrollView(); return false; }}); usernamelogin_password.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { changeScrollView(); return false; }}); /** * make ScrollView point bottom */ private void changeScrollView() {new Handler().postdelayed (new Runnable() {@override public void run() { mScrollView.scrollTo(0, mScrollView.getHeight()); }}, 300); }Copy the code



The 2

XML ditto

Create gon.xml in anim


Copy the code

visiable.xml

  
  Copy the code

Or directly in the code

import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.ScaleAnimation; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView mHead; // ImageView@override protected void onCreate(Bundle savedInstanceState) {super.oncreate (savedInstanceState); setContentView(R.layout.activity_main); mHead = (ImageView) findViewById(R.id.iv_head); final Button btn= (Button) findViewById(R.id.btn_usernamelogin_dologin); final EditText et_pass = (EditText) findViewById(R.id.et_usernamelogin_password); final EditText et_name = (EditText) findViewById(R.id.et_usernamelogin_username); SetOnTouchListener (new view.onTouchListener () {@override public Boolean onTouch(View v, MotionEvent event) { start(); return false; }}); btn.setEnabled(false); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } private void start() { AnimationSet animationSet = new AnimationSet(true); ScaleAnimation ScaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f, Animation.RELATIVE_TO_SELF, 0.1f, Animation. RELATIVE_TO_SELF, 0.5 f); scaleAnimation.setDuration(500); animationSet.addAnimation(scaleAnimation); animationSet.setFillAfter(true); animationSet.setFillBefore(false); animationSet.setRepeatCount(0); // Set the number of repetitions mhead. startAnimation(scaleAnimation); new Handler().postDelayed(new Runnable() { @Override public void run() { mHead.setVisibility(View.GONE); }}, 500); } @override public Boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { if(mHead.getVisibility()==View.GONE){ AnimationSet animationSet = new AnimationSet(true); ScaleAnimation ScaleAnimation = new ScaleAnimation(0.1f, 0.1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5 f); scaleAnimation.setDuration(500); animationSet.addAnimation(scaleAnimation); animationSet.setFillAfter(true); animationSet.setFillBefore(false); mHead.startAnimation(scaleAnimation); mHead.setVisibility(View.VISIBLE); }else { finish(); } } return false; }}Copy the code

Effect: