1.首先贴出这个页面的布局文件:
<?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"
android:background="#F7F7F7"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f7f7f7"
android:fadingEdge="none"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#f7f7f7"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ECECEC" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/white" >
<EditText
android:id="@+id/et_password"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@null"
android:layout_marginLeft="11dp"
android:hint="原密码"
android:singleLine="true"
android:textColor="@color/color_333333"
android:textColorHint="@color/color_c8c8c8"
android:textSize="14sp" />
<ToggleButton
android:id="@+id/tb_password"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:button="@drawable/switch_btn"
android:checked="true"
android:text=""
android:textOff=""
android:textOn="" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ECECEC" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/white" >
<EditText
android:id="@+id/et_new_password"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@null"
android:layout_marginLeft="11dp"
android:hint="新密码"
android:inputType="textPassword"
android:singleLine="true"
android:textColor="@color/color_333333"
android:textColorHint="@color/color_c8c8c8"
android:textSize="14sp" />
<ToggleButton
android:id="@+id/tb_new_password"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:button="@drawable/switch_btn"
android:checked="false"
android:text=""
android:textOff=""
android:textOn="" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ECECEC" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/white" >
<EditText
android:id="@+id/et_re_new_password"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@null"
android:layout_marginLeft="11dp"
android:hint="请再次输入新密码"
android:singleLine="true"
android:inputType="textPassword"
android:textColor="@color/color_333333"
android:textColorHint="@color/color_c8c8c8"
android:textSize="14sp" />
<ToggleButton
android:id="@+id/tb_re_new_password"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:button="@drawable/switch_btn"
android:checked="false"
android:text=""
android:textOff=""
android:textOn="" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ECECEC" />
<Button
android:layout_marginBottom="31dp"
android:layout_marginTop="23dp"
android:layout_width="281.5dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="@drawable/shape_red_tuichu"
android:text="保 存"
android:textColor="@color/color_ffffff"
android:textSize="17dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
android:textOff="" 未选中时按钮的文本.
android:textOn="" 选中时按钮的文本.
android:checked="true" 默认选中
android:checked="false" 默认未选中
效果如下:
![]()
2. ToggleButtond的android:button="@drawable/switch_btn"属性引用的布局文件switch_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/xiugaimima_xianshi" />
<item android:drawable="@drawable/xiugaimima_buxianshi" />
</selector>
3.代码控制密码框和文本框的切换,实现显示和隐藏密码:
import android.os.Bundle;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ToggleButton;
public class MYFragment extends BaseFragment {
private ToggleButton tb_password, tb_new_password, tb_re_new_password;
private EditText et_password, et_new_password, et_re_new_password;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
addContentView(R.layout.fragment_xgmm);
initListener();
return rootView;
}
@Override
protected void initViews() {
tb_password = (ToggleButton) findViewById(R.id.tb_password);
tb_new_password = (ToggleButton) findViewById(R.id.tb_new_password);
tb_re_new_password = (ToggleButton) findViewById(R.id.tb_re_new_password);
et_password = (EditText) findViewById(R.id.et_password);
et_new_password = (EditText) findViewById(R.id.et_new_password);
et_re_new_password = (EditText) findViewById(R.id.et_re_new_password);
}
private void initListener() {
tb_password.setOnCheckedChangeListener(mOnCheckedChangeListener);
tb_new_password.setOnCheckedChangeListener(mOnCheckedChangeListener);
tb_re_new_password.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
switch (id) {
case R.id.tb_password: {
if (isChecked) {
//普通文本框
et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//密码框
et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
et_password.postInvalidate();//刷新View
//将光标置于文字末尾
CharSequence charSequence = et_password.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
}
break;
case R.id.tb_new_password:
{
if (isChecked) {
et_new_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
et_new_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
et_new_password.postInvalidate();
CharSequence charSequence = et_new_password.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
}
break;
case R.id.tb_re_new_password:
{
if (isChecked) {
et_re_new_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
et_re_new_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
et_re_new_password.postInvalidate();
CharSequence charSequence = et_re_new_password.getText();
if (charSequence instanceof Spannable) {
Spannable spanText = (Spannable) charSequence;
Selection.setSelection(spanText, charSequence.length());
}
}
break;
}
}
};
}