最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現支付寶6位密碼輸入界面

 更新時間:2016年12月30日 16:21:39   作者:繁華穿越現實  
這篇文章主要為大家詳細介紹了Android實現支付寶6位密碼輸入界面的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

 我們先來照圖分析一下:
(1)限制輸入6位,每一位都有自己的框格,每個格顯示一位;
(2)有回退/取消支付按鈕;
(3)有忘記密碼鏈接;
(4)自定義的只能輸入數字的鍵盤輸入區(qū);
(5)在6位輸完后自動進行密碼校驗和支付交易。如上圖左邊是iOS支付寶支付密碼輸入控件,右邊是我模仿實現的效果。

首先,我們需要一個頁面來完成以上的靜態(tài)布局,.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#EEEEEE" 
 android:gravity="bottom"> 
 
 <LinearLayout 
  android:id="@+id/linear_pass" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="vertical"> 
 
  <RelativeLayout 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:layout_margin="5dp"> 
 
   <!-- 取消按鈕 --> 
   <ImageView 
    android:id="@+id/img_cancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/icon_clean" /> 
 
   <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="輸入密碼" 
    android:textColor="#898181" 
    android:textSize="20sp" /> 
  </RelativeLayout> 
 
  <View 
   android:layout_width="match_parent" 
   android:layout_height="0.5dp" 
   android:background="#555555" /> 
 
  <!-- 6位密碼框布局,需要一個圓角邊框的shape作為layout的背景 --> 
  <LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:layout_marginLeft="40dp" 
   android:layout_marginRight="40dp" 
   android:layout_marginTop="20dp" 
   android:background="@drawable/shape_input_area" 
   android:orientation="horizontal"> 
 
   <!-- inputType設置隱藏密碼明文 
     textSize設置大一點,否則“點”太小了,不美觀 --> 
   <TextView 
    android:id="@+id/tv_pass1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:inputType="numberPassword" 
    android:textSize="32sp" /> 
 
   <View 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:background="#999999" /> 
 
   <TextView 
    android:id="@+id/tv_pass2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:inputType="numberPassword" 
    android:textSize="32sp" /> 
 
   <View 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:background="#999999" /> 
 
   <TextView 
    android:id="@+id/tv_pass3" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:inputType="numberPassword" 
    android:textSize="32sp" /> 
 
   <View 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:background="#999999" /> 
 
   <TextView 
    android:id="@+id/tv_pass4" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:inputType="numberPassword" 
    android:textSize="32sp" /> 
 
   <View 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:background="#999999" /> 
 
   <TextView 
    android:id="@+id/tv_pass5" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:inputType="numberPassword" 
    android:textSize="32sp" /> 
 
   <View 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:background="#999999" /> 
 
   <TextView 
    android:id="@+id/tv_pass6" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:inputType="numberPassword" 
    android:textSize="32sp" /> 
  </LinearLayout> 
 
  <!-- 忘記密碼鏈接 --> 
  <TextView 
   android:id="@+id/tv_forgetPwd" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_gravity="right" 
   android:layout_margin="15dp" 
   android:text="忘記密碼?" 
   android:textColor="#354EEF" /> 
 </LinearLayout> 
 
 <!-- 輸入鍵盤 --> 
 <GridView 
  android:id="@+id/gv_keybord" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_below="@id/linear_pass" 
  android:layout_marginTop="40dp" 
  android:background="@android:color/black" 
  android:horizontalSpacing="0.5dp" 
  android:numColumns="3" 
  android:verticalSpacing="0.5dp" /> 
</RelativeLayout> 

其中需要圓角背景shape_input_area.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
 <corners android:radius="5dp"/> 
 <stroke android:color="@android:color/darker_gray" 
  android:width="1dp"/> 
 <solid android:color="@android:color/white"/> 
</shape> 

需要數字按鈕的背景selector_gride.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_enabled="false"> 
  <shape> 
   <solid android:color="#C0C4C7" /> 
  </shape> 
 </item> 
 <item android:state_enabled="true" android:state_pressed="false"> 
  <shape> 
   <solid android:color="@android:color/white" /> 
  </shape> 
 </item> 
 <item android:state_enabled="true" android:state_pressed="true"> 
  <shape> 
   <solid android:color="#C0C4C7" /> 
  </shape> 
 </item> 
</selector> 

需要回退鍵背景selector_key_del.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_enabled="false"> 
  <shape> 
   <solid android:color="#C0C4C7" /> 
  </shape> 
 </item> 
 <item android:state_enabled="true" android:state_pressed="false"> 
  <shape> 
   <solid android:color="#C0C4C7" /> 
  </shape> 
 </item> 
 <item android:state_enabled="true" android:state_pressed="true"> 
  <shape> 
   <solid android:color="@android:color/white" /> 
  </shape> 
 </item> 
</selector> 

下面來完成我們的自定義控件PasswordView.Java:

public class PasswordView extends RelativeLayout implements View.OnClickListener { 
 Context context; 
 
 private String strPassword;  //輸入的密碼 
 private TextView[] tvList;  //用數組保存6個TextView,為什么用數組? 
         //因為就6個輸入框不會變了,用數組內存申請固定空間,比List省空間(自己認為) 
 private GridView gridView; //用GrideView布局鍵盤,其實并不是真正的鍵盤,只是模擬鍵盤的功能 
 private ArrayList<Map<String, String>> valueList; //有人可能有疑問,為何這里不用數組了? 
              //因為要用Adapter中適配,用數組不能往adapter中填充 
 
 private ImageView imgCancel; 
 private TextView tvForget; 
 private int currentIndex = -1; //用于記錄當前輸入密碼格位置 
 
 public PasswordView(Context context) { 
  this(context, null); 
 } 
 
 public PasswordView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
  this.context = context; 
  View view = View.inflate(context, R.layout.layout_popup_bottom, null); 
   
  valueList = new ArrayList<Map<String, String>>(); 
  tvList = new TextView[6]; 
   
  imgCancel = (ImageView) view.findViewById(R.id.img_cancel); 
  imgCancel.setOnClickListener(this); 
 
  tvForget = (TextView) findViewById(R.id.tv_forgetPwd); 
  tvForget.setOnClickListener(this); 
   
  tvList[0] = (TextView) view.findViewById(R.id.tv_pass1); 
  tvList[1] = (TextView) view.findViewById(R.id.tv_pass2); 
  tvList[2] = (TextView) view.findViewById(R.id.tv_pass3); 
  tvList[3] = (TextView) view.findViewById(R.id.tv_pass4); 
  tvList[4] = (TextView) view.findViewById(R.id.tv_pass5); 
  tvList[5] = (TextView) view.findViewById(R.id.tv_pass6); 
 
  gridView = (GridView) view.findViewById(R.id.gv_keybord); 
 
  setView(); 
   
  addView(view);  //必須要,不然不顯示控件 
 } 
 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
   case R.id.img_cancel: 
    Toast.makeText(context, "Cancel", Toast.LENGTH_SHORT).show(); 
    break; 
   case R.id.tv_forgetPwd: 
    Toast.makeText(context, "Forget", Toast.LENGTH_SHORT).show(); 
    break; 
  } 
 } 
 
 private void setView() { 
  /* 初始化按鈕上應該顯示的數字 */ 
  for (int i = 1; i < 13; i++) { 
   Map<String, String> map = new HashMap<String, String>(); 
   if (i < 10) { 
    map.put("name", String.valueOf(i)); 
   } else if (i == 10) { 
    map.put("name", ""); 
   } else if (i == 12) { 
    map.put("name", "<<-"); 
   } else if (i == 11) { 
    map.put("name", String.valueOf(0)); 
   } 
   valueList.add(map); 
  } 
 
  gridView.setAdapter(adapter); 
  gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
   @Override 
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    if (position < 11 && position != 9) { //點擊0~9按鈕 
     if (currentIndex >= -1 && currentIndex < 5) {  //判斷輸入位置————要小心數組越界 
      tvList[++currentIndex].setText(valueList.get(position).get("name")); 
     } 
    } else { 
     if (position == 11) {  //點擊退格鍵 
      if (currentIndex - 1 >= -1) {  //判斷是否刪除完畢————要小心數組越界 
       tvList[currentIndex--].setText(""); 
      } 
     } 
    } 
   } 
  }); 
 } 
 
 //設置監(jiān)聽方法,在第6位輸入完成后觸發(fā) 
 public void setOnFinishInput(final OnPasswordInputFinish pass) { 
  tvList[5].addTextChangedListener(new TextWatcher() { 
   @Override 
   public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
 
   } 
 
   @Override 
   public void onTextChanged(CharSequence s, int start, int before, int count) { 
 
   } 
 
   @Override 
   public void afterTextChanged(Editable s) { 
    if (s.toString().length() == 1) { 
     strPassword = "";  //每次觸發(fā)都要先將strPassword置空,再重新獲取,避免由于輸入刪除再輸入造成混亂 
     for (int i = 0; i < 6; i++) { 
      strPassword += tvList[i].getText().toString().trim(); 
     } 
     pass.inputFinish(); //接口中要實現的方法,完成密碼輸入完成后的響應邏輯 
    } 
   } 
  }); 
 } 
 
 /* 獲取輸入的密碼 */ 
 public String getStrPassword() { 
  return strPassword; 
 } 
 
 /* 暴露取消支付的按鈕,可以靈活改變響應 */ 
 public ImageView getCancelImageView() { 
  return imgCancel; 
 } 
 
 /* 暴露忘記密碼的按鈕,可以靈活改變響應 */ 
 public TextView getForgetTextView() { 
  return tvForget; 
 } 
 
 //GrideView的適配器 
 BaseAdapter adapter = new BaseAdapter() { 
  @Override 
  public int getCount() { 
   return valueList.size(); 
  } 
 
  @Override 
  public Object getItem(int position) { 
   return valueList.get(position); 
  } 
 
  @Override 
  public long getItemId(int position) { 
   return position; 
  } 
 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 
   ViewHolder viewHolder; 
   if (convertView == null) { 
    convertView = View.inflate(context, R.layout.item_gride, null); 
    viewHolder = new ViewHolder(); 
    viewHolder.btnKey = (TextView) convertView.findViewById(R.id.btn_keys); 
    convertView.setTag(viewHolder); 
   } else { 
    viewHolder = (ViewHolder) convertView.getTag(); 
   } 
   viewHolder.btnKey.setText(valueList.get(position).get("name")); 
   if(position == 9){ 
    viewHolder.btnKey.setBackgroundResource(R.drawable.selector_key_del); 
    viewHolder.btnKey.setEnabled(false); 
   } 
   if(position == 11){ 
    viewHolder.btnKey.setBackgroundResource(R.drawable.selector_key_del); 
   } 
 
   return convertView; 
  } 
 }; 
 
 /** 
  * 存放控件 
  */ 
 public final class ViewHolder { 
  public TextView btnKey; 
 } 
} 

自認為代碼注釋還是可以的。就是在實現過程中要注意數組的越界問題,在輸入邏輯響應中要注意邏輯處理,也就是grideView的OnItemClickListener事件處理。其中用到自定義的接口OnPasswordInputFinish來實現輸入完成的事件回掉:

/** 
 * Belong to the Project —— MyPayUI 
 * Created by WangJ on 2015/11/25 17:15. 
 * 
 * 自定義接口,用于給密碼輸入完成添加回掉事件 
 */ 
public interface OnPasswordInputFinish { 
 void inputFinish(); 
} 

還有就是Adapter中用到的每個按鈕Item的布局item_gride.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 
 <!-- 模擬鍵盤按鈕,當然你可以用Button,但要注意Button和GrideView的點擊響應問題 --> 
 <TextView 
  android:id="@+id/btn_keys" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:padding="10dp" 
  android:gravity="center" 
  android:textSize="25sp" 
  android:background="@drawable/selector_gride"/> 
</LinearLayout> 

好了,到此我們的自定義控件——模仿支付寶6位支付密碼輸入控件就完成了,下邊我們在Activity中用一下,檢驗一下效果:
我們在MianActivity中用用一下我們定義好的控件:

public class MainActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
   
  /************* 第一種用法————開始 ***************/ 
  setContentView(R.layout.activity_main); 
 
  final PasswordView pwdView = (PasswordView) findViewById(R.id.pwd_view); 
   
  //添加密碼輸入完成的響應 
  pwdView.setOnFinishInput(new OnPasswordInputFinish() { 
   @Override 
   public void inputFinish() { 
    //輸入完成后我們簡單顯示一下輸入的密碼 
    //也就是說——>實現你的交易邏輯什么的在這里寫 
    Toast.makeText(MainActivity.this, pwdView.getStrPassword(), Toast.LENGTH_SHORT).show(); 
   } 
  }); 
   
  /** 
   * 可以用自定義控件中暴露出來的cancelImageView方法,重新提供相應 
   * 如果寫了,會覆蓋我們在自定義控件中提供的響應 
   * 可以看到這里toast顯示 "Biu Biu Biu"而不是"Cancel"*/ 
  pwdView.getCancelImageView().setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    Toast.makeText(MainActivity.this, "Biu Biu Biu", Toast.LENGTH_SHORT).show(); 
   } 
  }); 
  /************ 第一種用法————結束 ******************/ 
 
   
  /************* 第二種用法————開始 *****************/ 
//  final PasswordView pwdView = new PasswordView(this); 
//  setContentView(pwdView); 
//  pwdView.setOnFinishInput(new OnPasswordInputFinish() { 
//   @Override 
//   public void inputFinish() { 
//    Toast.makeText(MainActivity.this, pwdView.getStrPassword(), Toast.LENGTH_SHORT).show(); 
//   } 
//  }); 
  /************** 第二種用法————結束 ****************/ 
 } 
} 

在第一種方法中我們用到的布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
 android:id="@+id/xxx" 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#624762"> 
 
 <com.wangj.mypayview.PasswordView 
  android:id="@+id/pwd_view" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_alignParentBottom="true"/> 
</RelativeLayout> 

更多內容請參考專題:Android密碼使用教程

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 獲取控件大小和設置調整控件的位置XY示例

    獲取控件大小和設置調整控件的位置XY示例

    我需要的設置控件相對屏幕左上角的X 、Y位置,而不是自己本身位置的偏移,下面與大家介紹下怎么獲取設置控件的信息
    2013-06-06
  • 解析Android ANR問題

    解析Android ANR問題

    ANR 的全稱是 Application No Responding,即應用程序無響應,具體是一些特定的 Message (Key Dispatch、Broadcast、Service) 在應用的UI線程(主線程)沒有在規(guī)定的時間內處理完,進而觸發(fā) ANR 異常
    2021-01-01
  • Android Mms之:深入MMS支持

    Android Mms之:深入MMS支持

    本篇文章是對Android中MMS支持進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Android發(fā)送短信方法實例詳解

    Android發(fā)送短信方法實例詳解

    這篇文章主要介紹了Android發(fā)送短信方法,結合實例形式較為詳細的分析了Android發(fā)送短信的原理與具體實現技巧,需要的朋友可以參考下
    2016-01-01
  • 詳解Android如何設計一個全局可調用的ViewModel對象

    詳解Android如何設計一個全局可調用的ViewModel對象

    很多時候我們需要維護一個全局可用的ViewModel,因為這樣可以維護全局同一份數據源,且方便使用協(xié)程綁定App的生命周期,那如何設計全局可用的ViewModel對象,文中介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • 修改Android中hosts文件的步驟詳解

    修改Android中hosts文件的步驟詳解

    有朋友問Android怎么修改Hosts?對于這個問題,由于手頭并沒有Android設備,所以只能從網上搜羅了方法并總結出來,下面這篇文章主要介紹了修改Android中hosts文件的步驟,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • Android編程開發(fā)之RadioGroup用法實例

    Android編程開發(fā)之RadioGroup用法實例

    這篇文章主要介紹了Android編程開發(fā)之RadioGroup用法,結合實例形式分析了Android中RadioGroup單選按鈕的具體使用技巧,需要的朋友可以參考下
    2015-12-12
  • Ubuntu16.04 LTS 下安裝 Android Studio 2.2.2 的詳細步驟

    Ubuntu16.04 LTS 下安裝 Android Studio 2.2.2 的詳細步驟

    這篇文章主要介紹了Ubuntu16.04 LTS 下安裝 Android Studio 2.2.2 的詳細步驟,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Android用MVP實現一個簡單的類淘寶訂單頁面的示例

    Android用MVP實現一個簡單的類淘寶訂單頁面的示例

    本篇文章主要介紹了Android用MVP實現一個簡單的類淘寶訂單頁面的示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Android實現取消GridView中Item選中時默認的背景色

    Android實現取消GridView中Item選中時默認的背景色

    這篇文章主要介紹了Android實現取消GridView中Item選中時默認的背景色,涉及Android GridView中Item屬性設置的相關技巧,需要的朋友可以參考下
    2016-02-02

最新評論

安义县| 佛山市| 芷江| 固原市| 建始县| 定远县| 永安市| 团风县| 余江县| 汝州市| 清流县| 金寨县| 武强县| 马公市| 江油市| 灌云县| 观塘区| 汤阴县| 东港市| 巧家县| 安溪县| 清河县| 望谟县| 平谷区| 新干县| 灵山县| 开化县| 凭祥市| 吉木乃县| 随州市| 曲麻莱县| 淮滨县| 肥城市| 迁西县| 石柱| 云龙县| 中阳县| 喀喇沁旗| 屏南县| 湘潭县| 双牌县|