login介面 checkbox選擇顯示或者隱藏密碼

tangsilian發表於2016-04-14

這是效果

先來看看佈局檔案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);
                }

            }
        });

相關文章