一个使用Editview输入密码和确认密码的小程序
主程序:
package com.example.passwd;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText editText1 = null;
EditText editText2 = null;
String inputpwd = null;
String confirmpwd = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("activity start----->1");
editText1 = (EditText)findViewById(R.id.input_pwd);
editText2 = (EditText)findViewById(R.id.confirm_pwd);
//绑定监听软键盘的完成按键,使用遥控器时用重写onkeydown函数
editText1.setOnEditorActionListener(new EditText.OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
inputpwd = editText1.getText().toString();
System.out.println("key pressed");
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("activity start----->2");
if(inputpwd.length() < 4)
{
Toast.makeText(getApplicationContext(), "请输入四位密码",Toast.LENGTH_SHORT).show();
}
}
return false;
}
});
editText2.setOnEditorActionListener(new EditText.OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
inputpwd = editText1.getText().toString();
confirmpwd = editText2.getText().toString();
System.out.println("key pressed");
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("activity start----->3");
if(confirmpwd.length()<4)
{
Toast.makeText(getApplicationContext(), "请输入四位密码",Toast.LENGTH_SHORT).show();
}
else
{
if(inputpwd.equals(confirmpwd))
Toast.makeText(getApplicationContext(), "密码已修改为:" + confirmpwd,Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "两次输入密码不一致,请重新输入",Toast.LENGTH_SHORT).show();
}
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
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">
<TextView
android:id="@+id/input"
android:layout_width="260dp"
android:layout_height="40dp"
android:textSize="20sp"
android:paddingLeft="5dp"
android:gravity="center_vertical"
android:text="input password" />
<EditText
android:layout_alignParentRight="true"
android:layout_width="80dp"
android:layout_height="40dp"
android:imeOptions="flagNoExtractUi|actionDone"
android:inputType="numberPassword"
android:id="@+id/input_pwd"
android:maxLength="4"
android:textSize="20sp"
/>
<TextView
android:layout_width="260dp"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="confirm password"
android:paddingLeft="5dp"
android:layout_below="@+id/input"
android:textSize="20sp"
/>
<EditText
android:layout_alignParentRight="true"
android:layout_width="80dp"
android:layout_height="40dp"
android:imeOptions="flagNoExtractUi|actionDone"
android:inputType="numberPassword"
android:layout_below="@+id/input_pwd"
android:maxLength="4"
android:textSize="20sp"
android:id="@+id/confirm_pwd"
/>
</RelativeLayout>