标签:
<?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); } }
标签:
原文地址:http://www.cnblogs.com/gisoracle/p/5257603.html