login界面 checkbox选择显示或者隐藏密码

来源:互联网 发布:aws没有centos 编辑:程序博客网 时间:2024/06/11 10:23

这是效果

先来看看布局文件login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.demotest.MainActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="账号:" />        <EditText            android:id="@+id/edtlogin"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1000"            android:hint="输入账号" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密码:" />        <EditText            android:id="@+id/edtpassword"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:hint="输入密码" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="30dp" >        <CheckBox            android:id="@+id/checkbox"            android:layout_width="126dp"            android:layout_height="wrap_content"            android:text="显示密码" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/zhuchebtn"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="注册" />        <Button            android:id="@+id/loginbtn"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="登录" />    </LinearLayout></LinearLayout>

就是很简单的登录界面,我就不多解释了。
下面我们来看mainActivity.class的代码

public class MainActivity extends Activity {    private Button loginbtn;    private EditText edtname;    private EditText edtpassword;    private String name;    private String password;    private CheckBox checkbox;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.login);        loginbtn = (Button) findViewById(R.id.loginbtn);        edtname = (EditText) findViewById(R.id.edtlogin);        edtpassword = (EditText) findViewById(R.id.edtpassword);        checkbox = (CheckBox) findViewById(R.id.checkbox);        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if (!isChecked) {                    edtpassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);                } else {                    edtpassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);                }            }        });        loginbtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                name = edtname.getText().toString();                password = edtpassword.getText().toString();                // TODO Auto-generated method stub                if (name.equals("jaytang") && password.equals("4011")) {                    Intent intent = new Intent(MainActivity.this, Weixin.class);                    startActivity(intent);                } else {                    Toast.makeText(MainActivity.this, "false", Toast.LENGTH_SHORT).show();                }            }        });    }}

主要看那个checkbox的监听事件

 checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if (!isChecked) {                    edtpassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);                } else {                    edtpassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);                }            }        });
0 0
原创粉丝点击