Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android布局中對輸入密碼的隱藏和顯示

Android布局中對輸入密碼的隱藏和顯示

編輯:關於Android編程

 
    
    
    

package com.banana.k08.home;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    private EditText id,password;
    private CheckBox cb;
    private Button btn1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = (EditText) findViewById(R.id.tv1);
        password = (EditText) findViewById(R.id.password);
        cb = (CheckBox) findViewById(R.id.check);
        //設置選擇是否顯示密碼
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(cb.isChecked()){
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                else{
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
            }
        });

        findViewById(R.id.btnl).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String name = id.getText().toString();
                String pass = password.getText().toString();
                if (name.equals(admin) && pass.equals(admin)) {
                    Toast.makeText(getApplicationContext(),登陸成功,Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), 登陸失敗,請重新輸入, Toast.LENGTH_SHORT).show();

                }
            }
        });

    }


    
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved