TextView和EditView使用

来源:互联网 发布:同声翻译手机软件知乎 编辑:程序博客网 时间:2024/05/15 12:13

activity_main.xml布局代码如下:
整体采用相对布局,
局部使用线性布局

<RelativeLayout 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"    tools:context=".MainActivity"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"       >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_margin="10dp"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="用户名    "            android:textSize="20sp"            />        <EditText            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:inputType="phone"            android:drawableLeft="@mipmap/ic_launcher"            android:hint="请输入手机号码"            android:textSize="20sp"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_margin="10dp"        android:gravity="center">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密    码    "            android:textSize="20sp"            />        <EditText            android:id="@+id/editText2"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:hint="请输入密码"            android:textSize="20sp"            />    </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:layout_margin="10dp"            >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="证件号码"                android:textSize="20sp"                />            <EditText                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:digits="1234567890Xx"                android:hint="请输入身份证号"                android:textSize="20sp"                />        </LinearLayout>        <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="显示密码"            android:layout_gravity="right"/>    </LinearLayout>  <LinearLayout      android:layout_width="match_parent"      android:orientation="horizontal"     android:layout_alignParentBottom="true"      android:layout_height="wrap_content">      <TextView          android:id="@+id/textView1"          android:layout_width="0dp"          android:layout_height="wrap_content"          android:drawableTop="@mipmap/ic_launcher"          android:text="按钮"          android:gravity="center_horizontal"          android:layout_weight="1"         />      <TextView          android:layout_width="0dp"          android:layout_height="wrap_content"          android:layout_weight="1"          android:text="按钮"          android:gravity="center_horizontal"          android:drawableTop="@mipmap/ic_launcher"/>      <TextView          android:layout_width="0dp"          android:layout_height="wrap_content"          android:layout_weight="1"          android:text="按钮"          android:gravity="center_horizontal"          android:drawableTop="@mipmap/ic_launcher"/>      <TextView          android:layout_width="0dp"          android:layout_height="wrap_content"          android:layout_weight="1"          android:text="按钮"          android:gravity="center_horizontal"          android:drawableTop="@mipmap/ic_launcher"/>  </LinearLayout></RelativeLayout>

MainActivity.java代码如下

package com.example.administrator.mywidget;import android.app.Activity;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.method.PasswordTransformationMethod;import android.text.util.Linkify;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {    private TextView textView;    private Button button;    private EditText editText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button= (Button) findViewById(R.id.button);        editText= (EditText) findViewById(R.id.editText2);         button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //将密码变为可见   有这句话android:password="true"密码是不可见的,不加这句话是可见的                // editText.setTransformationMethod(null);                editText.setTransformationMethod(new PasswordTransformationMethod() );//不可见            }        });//        textView= (TextView) findViewById(R.id.textView1);//        textView.setTextColor(Color.BLUE);//        textView.setTextSize(50);//        textView.setAutoLinkMask(Linkify.PHONE_NUMBERS);//        textView.setText("我是电话号码13001153963");    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

运行界面如下图:
如果输入的密码是加密的,点击显示密码将显示自己输入的密码。
如果没有加密,点击显示密码将加密
这里写图片描述

0 0