<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"
> <!--0固定1可以扩展--> <TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="8"
android:shrinkColumns="0"
android:stretchColumns="1"> <TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入用户名:" />
<!-- 这个EditText放置在上边id为label的TextView的下边 -->
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label"
android:background="@android:drawable/editbox_background" /> </TableRow>
<TableRow> <TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<!-- 这个EditText放置在上边id为label的TextView的下边 -->
<EditText
android:id="@+id/pass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label1"
android:inputType="textPassword"
android:background="@android:drawable/editbox_background" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示密码" /> </TableRow> <TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:id="@+id/ok"
android:layout_column="1"
android:layout_span="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/cancel"
android:layout_column="1" /> </TableRow> </TableLayout> </LinearLayout>
package com.example.yanlei.mytk; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView passEdit;
private TextView userNameEdit;
private CheckBox checkBox1;
private Button btnok;
private Button btncancel; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passEdit = (TextView) findViewById(R.id.pass);
userNameEdit = (TextView) findViewById(R.id.username);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
btnok = (Button) findViewById(R.id.ok);
btncancel = (Button) findViewById(R.id.cancel); btncancel.setOnClickListener(new ButtonexitClickListener());
btnok.setOnClickListener(new ButtonokClickListener());
checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//如果选中,显示密码
passEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//否则隐藏密码
passEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
} }
}); } private class ButtonexitClickListener implements View.OnClickListener { public void onClick(View v) {
System.exit(0); } } private class ButtonokClickListener implements View.OnClickListener { public void onClick(View v) {
String UserName = userNameEdit.getText().toString();
String pass = passEdit.getText().toString();
Toast toast; if (UserName.equals("YL") && pass.equals("123")) {
toast = Toast.makeText(getApplicationContext(), "密码正确" + UserName + ":" + pass, Toast.LENGTH_LONG); } else {
toast = Toast.makeText(getApplicationContext(), "密码错误" + UserName + ":" + pass, Toast.LENGTH_LONG);
}
toast.show(); } } @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);
}
}
优化界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip"> <!--0固定1可以扩展--> <TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="8"
android:shrinkColumns="0"
android:stretchColumns="1"> <TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入用户名:" />
<!-- 这个EditText放置在上边id为label的TextView的下边 -->
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label"
android:background="@android:drawable/editbox_background" /> </TableRow> <TableRow> <TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<!-- 这个EditText放置在上边id为label的TextView的下边 -->
<EditText
android:id="@+id/pass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label1"
android:background="@android:drawable/editbox_background"
android:inputType="textPassword" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示密码" /> </TableRow> <TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"> </TableRow> </TableLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:gravity="right"
android:weightSum="1"> <Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="确定"
android:layout_marginRight="20dip" /> <Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:layout_weight="0.20"
android:hint="退出" android:clickable="true" />
</LinearLayout>
</LinearLayout>
=======================================================
又一个登陆界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="用户登录界面" android:autoText="false"
android:gravity="center" /> <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/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_weight="1"
android:inputType="textPassword" /> <CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示密码" android:id="@+id/checkBox" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="确认" /> <Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="返回" />
</LinearLayout> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/imageView"
android:layout_gravity="center_horizontal" /> </LinearLayout>