Android开发之EditText组件
输入特定字符
效果图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/user" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/pass" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/twopass" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/authcode" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow >
<EditText
android:layout_width="80dp"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/p2" />
<TextView
android:id="@+id/changeId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change" />
</TableRow>
</TableLayout>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/text"
android:id="@+id/textId"
/>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="提交注册" />
</RelativeLayout>
</LinearLayout>
////////////////strings.xml//////////////////////
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ZhuCeActivity!</string>
<string name="app_name">ZhuCe</string>
<string name="user">用户名:</string>
<string name="pass">密码:</string>
<string name="twopass">确认密码:</string>
<string name="email">电子邮箱:</string>
<string name="authcode">验证码:</string>
<string name="change">看不清?<a href="#">换一个</a></string>
<string name="text">我已经阅读并同意协议</string>
</resources>
自动完成输入内容的组件
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class AutoActivity extends Activity{
AutoCompleteTextView auto = null;
MultiAutoCompleteTextView mauto = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
String[] str = {"abc","add","aas","afr","bdr","ber","bsd"};
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line,str);
auto.setAdapter(adapter);
//----------------------
mauto.setAdapter(adapter);
mauto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private void findViews(){
auto = (AutoCompleteTextView) this.findViewById(R.id.autoId);
mauto = (MultiAutoCompleteTextView) this.findViewById(R.id.mautoId);
}
}
EditText中回车键的使用
为EditText对象的注册OnKeyListener事件,实现onKey()方法
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextTest1Activity extends Activity implements OnKeyListener{
/** Called when the activity is first created. */
Button but = null;
EditText et = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onkey);
findView();
}
public void findView(){
but = (Button) this.findViewById(R.id.buttonId);
et = (EditText) this.findViewById(R.id.edit);
et.setOnKeyListener(this);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_ENTER){
but.setText(et.getText());
et.setVisibility(View.GONE);
but.setVisibility(View.VISIBLE);
}
return false;
}
}