Android實(shí)現(xiàn)登陸頁(yè)logo隨鍵盤(pán)收放動(dòng)態(tài)伸縮(完美解決鍵盤(pán)彈出遮擋控件的問(wèn)題)
在最近的兩個(gè)項(xiàng)目中,項(xiàng)目需求要求我們實(shí)現(xiàn) /*登陸頁(yè)面的內(nèi)容能夠隨著鍵盤(pán)的彈出而被頂上去,避免鍵盤(pán)遮擋住登陸按鈕*/ 這樣的效果,寶寶心里苦呀,本來(lái)半天搞定的事還非得折騰一下,好吧我妥協(xié),畢竟我還是一只非常注重用戶(hù)體驗(yàn)的猿。
那就做吧,初步定下的方案是輸入框和登陸按鈕大小不變,在鍵盤(pán)彈出的時(shí)候讓logo的大小和位置進(jìn)行改變,從而給鍵盤(pán)騰出位置,當(dāng)然在鍵盤(pán)收起的時(shí)候還要給它還原一下,就像什么都沒(méi)發(fā)生一樣,嗯對(duì),就是這樣,說(shuō)了這么多,放張圖先感受一下效果吧:

接下來(lái)上正餐,布局上比較簡(jiǎn)單,注意給圖片外邊套上一個(gè)合身的linearlayout就好,因?yàn)榇龝?huì)要靠它改變logo的位置,布局代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".login.LoginActivity"
android:orientation="vertical"
android:id="@+id/ll_login_root">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:id="@+id/ll_login_logobg"
android:layout_marginBottom="50dp">
<ImageView
android:layout_width="160dp"
android:layout_height="160dp"
android:id="@+id/iv_login_logo"
android:background="@mipmap/login_logo"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="@mipmap/login_phone"
android:id="@+id/imageView2"
android:layout_gravity="bottom" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/et_login_phone"
android:layout_gravity="center"
android:hint="請(qǐng)輸入手機(jī)號(hào)"
android:background="@null"
android:maxLength="11"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/text_gray"></LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp" >
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:background="@mipmap/login_password"
android:id="@+id/imageView3"
android:layout_gravity="bottom" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/et_login_code"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="請(qǐng)輸入驗(yàn)證碼"
android:background="@null"
android:maxLength="6"/>
<Button
android:layout_width="90dp"
android:layout_height="30dp"
android:text="獲取驗(yàn)證碼"
android:textColor="@color/white"
android:id="@+id/bt_login_getcode"
android:background="@mipmap/login_button_blue"
android:layout_gravity="center_vertical"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/text_gray"
android:layout_marginBottom="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/ll_login_warning"
android:visibility="gone">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@mipmap/login_warning"
android:id="@+id/imageView"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="請(qǐng)輸入驗(yàn)證碼"
android:id="@+id/tv_login_wraning"
android:layout_gravity="center_vertical"
android:textColor="@color/text_red"
android:textSize="13dp" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="@color/white"
android:id="@+id/bt_login_submit"
android:background="@mipmap/login_button_gray"
android:text="登 錄"
android:textSize="18dp"
android:layout_marginTop="10dp" />
</LinearLayout>
</LinearLayout>
主代碼如下,我會(huì)把注釋添加到代碼中,因?yàn)槭钦麄€(gè)模塊的代碼所以也會(huì)有一些其他功能在里邊:
package com.millionideas.cm.login;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.millionideas.cm.R;
import com.millionideas.cm.home.HomeActivity;
import com.millionideas.cm.main.BaseActivity;
import com.millionideas.cm.tools.TimeCountUtils;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements View.OnLayoutChangeListener{
//用xUtils進(jìn)行控件綁定
@ViewInject(R.id.iv_login_logo)
ImageView iv_login_logo;
@ViewInject(R.id.ll_login_logobg)
LinearLayout ll_login_logobg;
@ViewInject(R.id.et_login_phone)
EditText et_login_phone;
@ViewInject(R.id.et_login_code)
EditText et_login_code;
@ViewInject(R.id.ll_login_warning)
LinearLayout ll_login_warning;
@ViewInject(R.id.tv_login_wraning)
TextView tv_login_wraning;
@ViewInject(R.id.bt_login_getcode)
Button bt_login_getcode;
@ViewInject(R.id.bt_login_submit)
Button bt_login_submit;
@ViewInject(R.id.ll_login_root)
LinearLayout activityRootView;//需要操作的布局
private TimeCountUtils timeCountUtils;
private Matcher phone_num;
private Pattern phonenumber;
private ProgressDialog progressDialog;
private Handler handler;
private int screenHeight = 0;//屏幕高度
private int keyHeight = 0; //軟件盤(pán)彈起后所占高度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); //獲取屏幕高度
keyHeight = screenHeight / 3;//彈起高度為屏幕高度的1/3
timeCountUtils = new TimeCountUtils(LoginActivity.this, 60000, 1000, bt_login_getcode);//時(shí)間工具類(lèi)用以實(shí)現(xiàn)倒計(jì)時(shí)
progressDialog=new ProgressDialog(this);//對(duì)話(huà)框
handler=new Handler();
bt_login_submit.setClickable(false);
et_login_phone.addTextChangedListener(new TextWatcher() {//為edittext添加文本改變監(jiān)聽(tīng),根據(jù)是否有文本輸入更改確認(rèn)按鈕的背景顏色
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!et_login_phone.getText().toString().equals("")){
bt_login_submit.setClickable(true);
bt_login_submit.setBackgroundResource(R.drawable.login_button);
}else {
bt_login_submit.setClickable(false);
bt_login_submit.setBackgroundResource(R.mipmap.login_button_gray);
}
}
});
}
//xUtils的事件處理
@Event(value = {R.id.bt_login_submit, R.id.bt_login_getcode}, type = View.OnClickListener.class)
private void onClick(View view) {
switch (view.getId()) {
case R.id.bt_login_submit://確認(rèn)按鈕事件
if (!CheckPhone(et_login_phone).matches()) {//判斷手機(jī)號(hào)格式
ll_login_warning.setVisibility(View.VISIBLE);
tv_login_wraning.setText("手機(jī)號(hào)碼格式不正確");
} else if (!et_login_code.getText().toString().equals("000")) {//驗(yàn)證碼判斷,為方便測(cè)試設(shè)置了默認(rèn)值
ll_login_warning.setVisibility(View.VISIBLE);
tv_login_wraning.setText("驗(yàn)證碼不正確");
} else {//條件全部滿(mǎn)足,開(kāi)始登陸
ll_login_warning.setVisibility(View.GONE);
progressDialog.setMessage("正在登錄…");
progressDialog.show();//彈出加載對(duì)話(huà)框
handler.postDelayed(new Runnable() {//設(shè)置一個(gè)1s的延時(shí)操作模擬登陸的過(guò)程
@Override
public void run() {//登陸成功關(guān)掉對(duì)話(huà)框,跳轉(zhuǎn)頁(yè)面,關(guān)掉本頁(yè)
progressDialog.dismiss();//不能用hide
Intent intent=new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
},1000);
}
break;
case R.id.bt_login_getcode:
if (CheckPhone(et_login_phone).matches()) {//手機(jī)號(hào)正確則獲取驗(yàn)證碼,開(kāi)啟倒計(jì)時(shí)
ll_login_warning.setVisibility(View.GONE);
bt_login_getcode.setBackgroundResource(R.mipmap.login_button_gray);
timeCountUtils.start();
} else {
ll_login_warning.setVisibility(View.VISIBLE);
tv_login_wraning.setText("手機(jī)號(hào)碼格式不正確");
}
break;
}
}
public Matcher CheckPhone(EditText editText) {//判斷手機(jī)號(hào)格式
phonenumber = Pattern
.compile("^[1][3-8][0-9]{9}$");
phone_num = phonenumber.matcher(editText.getText()
.toString());
return phone_num;
}
@Override
protected void onResume() {
super.onResume();
activityRootView.addOnLayoutChangeListener(this);//給需要操作的布局設(shè)置監(jiān)聽(tīng)
}
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
/* old是改變前的左上右下坐標(biāo)點(diǎn)值,沒(méi)有old的是改變后的左上右下坐標(biāo)點(diǎn)值
現(xiàn)在認(rèn)為只要控件將Activity向上推的高度超過(guò)了1/3屏幕高,就認(rèn)為軟鍵盤(pán)彈起*/
if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
ViewGroup.LayoutParams params = iv_login_logo.getLayoutParams();//獲取布局,設(shè)置鍵盤(pán)彈起后logo的寬高
params.height = 300;
params.width = 300;
iv_login_logo.setLayoutParams(params);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ll_login_logobg.getLayoutParams());
lp.setMargins(0, 90, 0, 50);//設(shè)置包含logo的布局的位置
ll_login_logobg.setLayoutParams(lp);
} else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {//鍵盤(pán)收回后,logo恢復(fù)原來(lái)大小,位置同樣回到初始位置
ViewGroup.LayoutParams params = iv_login_logo.getLayoutParams();
params.height = 480;
params.width = 480;
iv_login_logo.setLayoutParams(params);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ll_login_logobg.getLayoutParams());
lp.setMargins(0, 270, 0, 150);
ll_login_logobg.setLayoutParams(lp);
}
}
}
以上所述是小編給大家介紹的Android實(shí)現(xiàn)登陸頁(yè)logo隨鍵盤(pán)收放動(dòng)態(tài)伸縮(完美解決鍵盤(pán)彈出遮擋控件的問(wèn)題),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
android獲取手機(jī)唯一標(biāo)識(shí)的方法
這篇文章主要介紹了獲取安卓的手機(jī)或者平板的唯一標(biāo)識(shí)的方法,需要的朋友可以參考下2014-02-02
android開(kāi)發(fā)基礎(chǔ)教程—打電話(huà)發(fā)短信
打電話(huà)發(fā)短信的功能已經(jīng)離不開(kāi)我們的生活了,記下來(lái)介紹打電話(huà)發(fā)短信的具體實(shí)現(xiàn)代碼,感興趣的朋友可以了解下2013-01-01
Android自定義gridView仿頭條頻道拖動(dòng)管理功能
這篇文章主要介紹了Android自定義gridView仿頭條頻道拖動(dòng)管理功能,本文通過(guò)實(shí)例代碼效果圖展示給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Android列表動(dòng)圖展示的實(shí)現(xiàn)策略
這篇文章主要給大家介紹了關(guān)于Android列表動(dòng)圖展示的實(shí)現(xiàn)策略的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Flutter實(shí)現(xiàn)自定義搜索框AppBar的示例代碼
開(kāi)發(fā)中,頁(yè)面頭部為搜索樣式的設(shè)計(jì)非常常見(jiàn),為了可以像系統(tǒng)AppBar那樣使用,本文將利用Flutter自定義一個(gè)搜索框,感興趣的可以了解一下2022-04-04
Android實(shí)現(xiàn)繪制折線圖APP代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)繪制折線圖APP代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2022-02-02
Android activity堆棧及管理實(shí)例詳解
這篇文章主要介紹了Android activity堆棧及管理實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)android activity堆棧相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-09-09
Android Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹
這篇文章主要介紹了Android Intent傳遞數(shù)據(jù)底層分析詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02

