package
secondriver.viewlibrary;
import
android.content.Context;
import
android.content.res.TypedArray;
import
android.text.InputType;
import
android.text.method.HideReturnsTransformationMethod;
import
android.text.method.PasswordTransformationMethod;
import
android.util.AttributeSet;
import
android.view.ViewGroup;
import
android.widget.CheckBox;
import
android.widget.CompoundButton;
import
android.widget.EditText;
import
android.widget.LinearLayout;
/**
* PassWordView
* <p/>
* leftComponent是一个EditText
* RightComponent是一个CheckBox
* <p/>
* Author : secondriver
* Created : 2015/11/25
*/
public
class
PassWordView
extends
LinearLayout {
private
EditText mPassWordEditText;
private
CheckBox mShowCheckBox;
private
LinearLayout.LayoutParams mPassWordParams;
private
LinearLayout.LayoutParams mShowParams;
private
float
leftComponentWeight;
private
float
rightComponentWeight;
private
int
inputType;
public
PassWordView(Context context, AttributeSet attrs) {
super
(context, attrs);
initProperty(context, attrs);
initComponent(context);
}
private
final
void
initProperty(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PassWordView);
leftComponentWeight = typedArray.getFloat(R.styleable.PassWordView_left_component_weight,
5
.0f);
rightComponentWeight = typedArray.getFloat(R.styleable.PassWordView_right_component_weight,
1
.0f);
inputType = typedArray.getInt(R.styleable.PassWordView_android_inputType, InputType.TYPE_TEXT_VARIATION_PASSWORD);
typedArray.recycle();
}
private
void
initComponent(Context context) {
mPassWordEditText =
new
EditText(context);
mPassWordEditText.setInputType(inputType);
mPassWordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
mShowCheckBox =
new
CheckBox(context);
mPassWordParams =
new
LinearLayout.LayoutParams(
0
, ViewGroup.LayoutParams.WRAP_CONTENT, leftComponentWeight);
mShowParams =
new
LinearLayout.LayoutParams(
0
, ViewGroup.LayoutParams.WRAP_CONTENT, rightComponentWeight);
setOrientation(HORIZONTAL);
addView(mPassWordEditText, mPassWordParams);
addView(mShowCheckBox, mShowParams);
addCheckBoxListener();
}
/**
* 获取PassWord
*
* @return
*/
public
String getPassWord() {
return
mPassWordEditText.getText().toString();
}
/**
* 获取PassWord组件
*
* @return
*/
public
EditText getPassWordEditText() {
return
mPassWordEditText;
}
private
final
void
addCheckBoxListener() {
/**
* CheckBox点击事件处理
*
* 如果选中EditText中的密码明文显示
*
* 如果未选中EditText中的密码黑点显示
*
*/
mShowCheckBox.setOnCheckedChangeListener(
new
CompoundButton.OnCheckedChangeListener() {
@Override
public
void
onCheckedChanged(CompoundButton buttonView,
boolean
isChecked) {
if
(isChecked) {
mPassWordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else
{
mPassWordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
}