編輯:關於Android編程
前言
比如在進行登錄的操作中,用戶輸入完密碼之後,肯定是想直接點擊登錄按鈕的。返回鍵隱藏軟鍵盤這樣的體驗肯定很糟糕,程序員,遇到問題解決問題。

實現1
xml
<ScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="wrap_content" android:fadingEdge="none" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:src="@mipmap/ic_loginhead"/> <EditText android:id="@+id/et_usernamelogin_username" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="10dp" android:background="@null" android:hint="請輸入已驗證手機" android:inputType="number" android:lines="1" android:maxLength="11"/> <ImageView android:layout_width="match_parent" android:layout_height="2px" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:background="@color/pating_line"/> <EditText android:id="@+id/et_usernamelogin_password" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="20dp" android:background="@null" android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_?" android:hint="請輸入密碼" android:inputType="textPassword"/> <ImageView android:layout_width="match_parent" android:layout_height="2px" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:background="@color/pating_line"/> <Button android:id="@+id/btn_usernamelogin_dologin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" android:background="@drawable/btn_selecter" android:enabled="false" android:text="登錄" android:textColor="@color/white" /> </LinearLayout> </ScrollView>
java
mScrollView=(ScrollView)view.findViewById(R.id.scrollview);
usernamelogin_username.setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
changeScrollView();
returnfalse;
}
});
usernamelogin_password.setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
changeScrollView();
returnfalse;
}
});
/**
*使ScrollView指向底部
*/
privatevoidchangeScrollView(){
newHandler().postDelayed(newRunnable(){
@Override
publicvoidrun(){
mScrollView.scrollTo(0,mScrollView.getHeight());
}
},300);
}

實現2
xml同上
anim下新建gone.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="500" android:repeatCount="0"/>
visiable.xml
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="500" android:repeatCount="0"/>
或者直接在代碼中
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.KeyEvent;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationSet;
importandroid.view.animation.ScaleAnimation;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
publicclassMainActivityextendsAppCompatActivity{
privateImageViewmHead;//頭部ImageView
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHead=(ImageView)findViewById(R.id.iv_head);
finalButtonbtn=(Button)findViewById(R.id.btn_usernamelogin_dologin);
finalEditTextet_pass=(EditText)findViewById(R.id.et_usernamelogin_password);
finalEditTextet_name=(EditText)findViewById(R.id.et_usernamelogin_username);
/**
*當輸入被點擊
*/
et_name.setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
start();
returnfalse;
}
});
btn.setEnabled(false);
btn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
}
});
}
privatevoidstart(){
AnimationSetanimationSet=newAnimationSet(true);
ScaleAnimationscaleAnimation=newScaleAnimation(
1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(500);
animationSet.addAnimation(scaleAnimation);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setRepeatCount(0);//設置重復次數
mHead.startAnimation(scaleAnimation);
newHandler().postDelayed(newRunnable(){
@Override
publicvoidrun(){
mHead.setVisibility(View.GONE);
}
},500);
}
/**
*菜單、返回鍵響應
*/
@Override
publicbooleanonKeyDown(intkeyCode,KeyEventevent){
//TODOAuto-generatedmethodstub
if(keyCode==KeyEvent.KEYCODE_BACK){
if(mHead.getVisibility()==View.GONE){
AnimationSetanimationSet=newAnimationSet(true);
ScaleAnimationscaleAnimation=newScaleAnimation(
0.1f,1f,0.1f,1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(500);
animationSet.addAnimation(scaleAnimation);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
mHead.startAnimation(scaleAnimation);
mHead.setVisibility(View.VISIBLE);
}else{
finish();
}
}
returnfalse;
}
}
效果呢:
以上所述是小編給大家介紹的Android優雅的方式解決軟鍵盤遮擋按鈕問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
華為榮耀note8怎麼預約 榮耀note8預約購買攻略
華為榮耀於8月1號下午正式發布了6.6吋大屏手機華為榮耀NOTE8,那麼想要購買新機的朋友是不是很想知道華為榮耀note8怎麼預約購買呢?下面小編就馬上帶來
AndroidStudio0.5.2 BUG 導致 menu 菜單鍵崩潰
郁悶了半天,今天發現一點擊手機 menu 鍵應用就崩潰了,記得之前都是好好的,調試了半天代碼還是搞不定,於是網上google了一番,發現僅國外有一兩篇文章有提到類
GraphicsLab Project之輝光(Glare,Glow)效果
引言從GraphicsLab Project項目立項以來,一直都在忙著搭建Shader的實驗環境,現在基本的實驗環境已經搭建完畢,所以就試著使用它來編寫一些效果。本篇文章
Android圓角圖片最簡單的實現方法詳解
1. 前言:在平時的開發中,我們在顯示圖片是有時候需要顯示圓角圖片,我們應該都知道圓角顯示肯定是更加耗費內存和性能,會導致圖片的過度繪制等問題。但是有時候產品的設計就是這