Android 사용자 정의 View 모방 알 리 페 이 6 비트 비밀번호 입력 기능

14544 단어 androidview
카드 화면 을 선택 하 는 것 과 비슷 하고 PopupWindow 를 사용 합 니 다.그러나 암호 화면 을 입력 하 는 것 은 사용자 정의 view 입 니 다.여섯 개의 비밀 번 호 를 입력 한 후에 Activity 에서 입력 한 비밀 번 호 를 되 돌려 서 Toast 로 비밀 번 호 를 표시 합 니 다.효과 도 는 다음 과 같다.

사용자 정의 view 레이아웃 효과 그림 및 코드 는 다음 과 같 습 니 다.

<?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="wrap_content"
android:background="@drawable/bg_pop_window"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_main_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/iv_pay_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/back_white"/>
<TextView
android:id="@+id/tv_pay_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="  "
android:textColor="#333"
android:textSize="18dp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#e5e5e5"/>
<!-- 6      ,         shape  layout    -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
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:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<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:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<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:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<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:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<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:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#e5e5e5"/>
<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:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="32sp"/>
</LinearLayout>
<TextView
android:id="@+id/tv_pay_forgetPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="15dp"
android:text="    ?"
android:textColor="#354EEF"/>
<!--      -->
<GridView
android:id="@+id/gv_keybord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_main_password"
android:layout_marginTop="30dp"
android:horizontalSpacing="0.5dp"
android:background="#8E8E8E"
android:numColumns="3"
android:verticalSpacing="0.5dp"/>
</LinearLayout>
</RelativeLayout>
자바 코드

/**
* Created by zhpan on 2016/9/25.
*/
public class PayView extends RelativeLayout{
private MainActivity mContext;
private String mStringPassword; //     
private TextView[] mTextViewPsw; //      6 TextView
private GridView mGridView; //      
private ArrayList<Map<String, String>> valueList;
private ImageView mImageViewCancel;
private TextView mTextViewForgetPsw;
private int currentIndex = -1;//              
private View mView;
private TextView mTextViewTitle;
private TextView mTextViewDel;
public PayView(Context context) {
super(context, null);
}
public PayView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = (MainActivity) context;
mView = View.inflate(context, R.layout.pay_view, null);
valueList = new ArrayList<>();
mTextViewPsw = new TextView[6];
mImageViewCancel = (ImageView) mView.findViewById(R.id.iv_pay_back);
mTextViewPsw[0] = (TextView) mView.findViewById(R.id.tv_pass1);
mTextViewPsw[1] = (TextView) mView.findViewById(R.id.tv_pass2);
mTextViewPsw[2] = (TextView) mView.findViewById(R.id.tv_pass3);
mTextViewPsw[3] = (TextView) mView.findViewById(R.id.tv_pass4);
mTextViewPsw[4] = (TextView) mView.findViewById(R.id.tv_pass5);
mTextViewPsw[5] = (TextView) mView.findViewById(R.id.tv_pass6);
mGridView = (GridView) mView.findViewById(R.id.gv_keybord);
mTextViewTitle = (TextView) mView.findViewById(R.id.tv_pay_title);
mTextViewForgetPsw = (TextView) mView.findViewById(R.id.tv_pay_forgetPwd);
setView();
addView(mView); //   ,       
}
//              
private void setView() {
for (int i = 1; i < 13; i++) {
Map<String, String> map = new HashMap<>();
if (i < 10) {
map.put("name", String.valueOf(i));
} else if (i == 10) {
map.put("name", "");
} else if (i == 11) {
map.put("name", String.valueOf(0));
} else if (i == 12) {
map.put("name", "<-");
}
valueList.add(map);
}
mGridView.setAdapter(adapter);
}
/**
*       ,  6       
*/
public void setOnFinishInput(final OnPasswordInputFinish pass) {
mTextViewPsw[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) {
mStringPassword = ""; //       mStringPassword       ,               
for (int i = 0; i < 6; i++) {
mStringPassword += mTextViewPsw[i].getText().toString().trim();
}
pass.inputFinish();//         ,              
}
}
});
}
/**
*        
*/
public String getPassword() {
return mStringPassword;
}
/**
*        ImageView
*/
public ImageView getCancel() {
return mImageViewCancel;
}
/**
*        TextView
*/
public TextView getForgetPsw() {
return mTextViewForgetPsw;
}
/**
*      TextView
*/
public TextView getTitle() {
return mTextViewTitle;
}
// GridView    
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(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_pay_gride, null);
holder = new ViewHolder();
holder.btnKey = (TextView) convertView.findViewById(R.id.btn_keys);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnKey.setText(valueList.get(position).get("name"));
if (position == 9) {
holder.btnKey.setBackgroundResource(R.drawable.selector_key_del);
}
if (position == 11) {
mTextViewDel = holder.btnKey;
holder.btnKey.setBackgroundResource(R.drawable.selector_key_del);
}
holder.btnKey.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (position < 11 && currentIndex != 9&&position!=9) { //  0-9  
if (currentIndex >= -1 && currentIndex < 5) { //      
mTextViewPsw[++currentIndex].setText(valueList.get(position).get("name"));
}
} else {
if (position == 11) { //     
if (currentIndex - 1 >= -1) { //         
mTextViewPsw[currentIndex--].setText("");
}
}
if(position==9){
}
}
}
});
return convertView;
}
};
static class ViewHolder {
public TextView btnKey;
}
}
PopupWindow 에서 이 컨트롤 을 직접 사용 합 니 다.

<?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">
<com.example.zhpan.mypayui.PayView
android:id="@+id/pv_pop_win"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity 에서 PupPupWindow 보이 기

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTextView;
private PopupWindow mPopupWindow;
private View popView;
private PayView mPayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
private void initView() {
mTextView = (TextView) findViewById(R.id.tv_main_pay);
}
private void setListener() {
mTextView.setOnClickListener(this);
}
//     
public void showPopupWindow() {
//      
popView = View.inflate(this, R.layout.pop_window, null);
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mPayView = (PayView) popView.findViewById(R.id.pv_pop_win);
mPayView.getTitle().setText("       ");
//     
mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
mPopupWindow.showAsDropDown(findViewById(R.id.ll_main), 0, 0);
mPopupWindow.setOutsideTouchable(true);
mPayView.setOnFinishInput(new OnPasswordInputFinish() {
@Override
public void inputFinish() {
Toast.makeText(MainActivity.this, mPayView.getPassword(), Toast.LENGTH_SHORT).show();
}
});
mPayView.getCancel().setOnClickListener(this);
mPayView.getForgetPsw().setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_main_pay:
showPopupWindow();
break;
case R.id.iv_pay_back:
mPopupWindow.dismiss();
break;
case R.id.tv_pay_forgetPwd:
Toast.makeText(MainActivity.this,"    ",Toast.LENGTH_SHORT).show();
break;
}
}
}
위 에서 말 한 것 은 소 편 이 소개 한 안 드 로 이 드 사용자 정의 View 모 의 알 리 페 이 6 비트 비밀번호 입력 기능 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.소 편 은 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기