1. 程式人生 > 其它 >app直播原始碼,android實現帶下劃線的密碼輸入框

app直播原始碼,android實現帶下劃線的密碼輸入框

app直播原始碼,android實現帶下劃線的密碼輸入框實現的相關程式碼

現在很多app的密碼輸入框,都採用微信、支付寶等密碼輸入框的樣式。還有一種就是每個密碼字元下面帶有一條下劃線的樣式。

仿微信、支付寶網上搜了下有很多demo,但是帶下劃線的不多,或者講的比較複雜,都是自定義什麼的。這兩天正好要做個這樣的東西,研究了一下,感覺沒那麼麻煩,不需要各種自定義。我的思路是:

1). 佈局中定義一個edittext, 全透明,輸入的字元大小為0sp, 這樣使用者就看不見。不能設定為visibility=gone,否則無法獲取焦點,彈 不出輸入法。

2). 然後定義四個textview, 來顯示edittext的輸入。同時定義四個下劃線,分別在每個textview的下方。

3). 監聽edittext的輸入事件,控制每個textview的顯示

1. 佈局檔案:


<?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">
<TextView
android:id="@+id/input_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="請輸入密碼"
android:textSize="20sp"
android:textColor="#333333"/>
<EditText
android:id="@+id/input_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="number"
android:digits="0123456789"
android:maxLength="4"
android:maxLines="1"
android:background="@null"
android:cursorVisible="false"
android:textColor="#00000000"
android:textSize="0sp"
android:visibility="visible"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/input_title"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginRight="45dp"
android:paddingBottom="10dp">
<TextView
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="number"
android:digits="0123456789"
android:maxLength="1"
android:maxLines="1"
android:background="@null"
android:cursorVisible="false"
android:textColor="#333333"
android:textSize="32sp"/>
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#767676"
android:layout_marginTop="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginRight="45dp"
android:paddingBottom="10dp">
<TextView
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="number"
android:digits="0123456789"
android:maxLength="1"
android:maxLines="1"
android:background="@null"
android:cursorVisible="false"
android:textColor="#333333"
android:textSize="32sp"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#767676"
android:layout_marginTop="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginRight="45dp"
android:paddingBottom="10dp">
<TextView
android:id="@+id/edit3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLength="1"
android:maxLines="1"
android:background="@null"
android:cursorVisible="false"
android:textColor="#333333"
android:textSize="32sp"/>
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#767676"
android:layout_marginTop="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="10dp">
<TextView
android:id="@+id/edit4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:inputType="number"
android:digits="0123456789"
android:maxLength="1"
android:maxLines="1"
android:background="@null"
android:cursorVisible="false"
android:textColor="#333333"
android:textSize="32sp"/>
<TextView
android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#767676"
android:layout_marginTop="12dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

2. 實現程式碼

private Dialog inputDialog;
private LayoutInflater mInflater;
private EditText input_edit;
private TextView edit1;
private TextView edit2;
private TextView edit3;
private TextView edit4;
private String codeStr = null;
private void initInputDialogView(View view)
{
input_edit = (EditText) view.findViewById(R.id.input_edit);
input_edit.addTextChangedListener(mTextWatcher);

edit1 = (TextView) view.findViewById(R.id.edit1);
edit2 = (TextView) view.findViewById(R.id.edit2);
edit3 = (TextView) view.findViewById(R.id.edit3);
edit4 = (TextView) view.findViewById(R.id.edit4);

edit1.setOnClickListener(onClickListener);
edit2.setOnClickListener(onClickListener);
edit3.setOnClickListener(onClickListener);
edit4.setOnClickListener(onClickListener);
}
//建立輸入框dialog
private void createInputDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if (mInflater == null)
{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
View view = mInflater.inflate(R.layout.input_view_layout, null);
initInputDialogView(view);
builder.setView(view);
inputDialog = null;
inputDialog = builder.create();
inputDialog.show();
openKeyboard(input_edit); //dialog show之後立即彈出輸入法
}
//對四個顯示密碼textview監聽點選事件,這樣做是為了:如果輸入法沒有彈起來,使用者點選密碼框也是可以彈起來的
private View.OnClickListener onClickListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int vid = v.getId();
switch (vid)
{
case R.id.edit1:
case R.id.edit2:
case R.id.edit3:
case R.id.edit4:
openKeyboard(input_edit);
break;
}
}
}
//輸入監聽
private TextWatcher mTextWatcher = 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)
{
/**
* 我的處理邏輯是:1. 只能從第一個框輸入;2.只能從最後一次輸入的框刪除
* 3. 如果輸入的密碼個數小於4(我這裡總共4個), 也是可以刪除的。4.按了刪除鍵,還可以繼續接著輸入,保證每一次輸入,顯示的位置都是對的。
**/
int len = s.length();
String str = s.toString();
codeStr = str;
switch (len)
{
case 1:
edit1.setText(str);
edit2.setText("");
edit3.setText("");
edit4.setText("");
break;
case 2:
edit2.setText(subStr(str, 1, 2));
edit3.setText("");
edit4.setText("");
break;
case 3:
edit3.setText(subStr(str, 2, 3));
edit4.setText("");
break;
case 4:
edit4.setText(subStr(str, 3, 4));
break;
default:
edit1.setText("");
break;
}
}
};
private String subStr(String s, int start, int end)
{
if (s == null || s.length() < 1)
{
return "";
}
return s.substring(start, end);
}
//彈起輸入法
public static void openKeyboard(final EditText view)
{
view.setFocusableInTouchMode(true);
view.setFocusable(true);
view.setClickable(true);
view.requestFocus();
InputMethodManager inputManager = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, 0);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
InputMethodManager inputManager=(InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view,0);
}
}, 500);
}

3. 最後在合適的地方呼叫createInputDialog()方法就能彈出密碼框,並且同時彈起輸入法。

以上就是 app直播原始碼,android實現帶下劃線的密碼輸入框實現的相關程式碼,更多內容歡迎關注之後的文章