码迷,mamicode.com
首页 > 移动开发 > 详细

Android布局中对输入密码的隐藏和显示

时间:2015-05-26 00:21:42      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:布局   android   

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:layout_weight="0.91" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:id="@+id/textView1"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/password"
            android:inputType="textPassword"/>
    </LinearLayout>
    <CheckBox
        android:id="@+id/check"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示密码" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:id="@+id/btnl"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="100"
            android:id="@+id/textView2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:layout_toEndOf="@+id/textView2"
            android:layout_gravity="right" />
    </LinearLayout>

</LinearLayout>

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();

                }
            }
        });

    }


    
}


Android布局中对输入密码的隐藏和显示

标签:布局   android   

原文地址:http://blog.csdn.net/u010304217/article/details/45998351

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!