EditText 类是 TextView 的子类,同时 EditText 类又派生出两个子类 AutoCompleteTextView 和 ExtractEditText。层次关系如下:
java.lang.Object;
android.view.View;
android.widget.TextView;
android.widget.EditText;
AutoCompleteTextView 和 ExtractEditText
EditText(context); EditText(context, attrs); EditText(context, attrs, defStyle); public void extendSelection(int index); public void selectAll(); public void setEllipsize(TextUtils.TruncateAt ellipsis); public void setSelection(int index); public void setSelection(int start, int stop); public void getDefaultSize(int size, int measureSpec); protected MovementMethod getDefaultMovementMethod();以上方法均是自身方法,下面给出其他常用方法
完整工程:http://download.csdn.net/detail/sweetloveft/9385096
下述程序,主要作用是了解 EditText 的用法。
重点注意:EditText 中的 OnEditorAction 需要注意只有在输入 Enter 后才会被触发!
package com.sweetlover.activity; import com.sweetlover.edittextdemo.R; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText editPhone = (EditText)findViewById(R.id.ET_phonenumber); EditText editPassword = (EditText)findViewById(R.id.ET_password); // 在内部类的其他方法中不可以引用本类中的非 final 变量 final TextView text = (TextView)findViewById(R.id.myTextView); editPhone.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // TODO Auto-generated method stub text.setText("Editing phone number"); return false; } }); editPassword.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // TODO Auto-generated method stub text.setText("Editing password"); return false; } }); } }
<?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:id="@+id/myTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/ET_phonenumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLength="40" android:hint="@string/phone_number" android:textColorHint="@color/BLACK" android:inputType="phone" android:imeOptions="actionGo"/> <EditText android:id="@+id/ET_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLength="40" android:hint="@string/password" android:textColorHint="@color/BLACK" android:inputType="textPassword" android:imeOptions="actionSearch"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="BLACK">#FF000000</color> </resources>
<resources> <string name="app_name">EditTextDemo</string> <string name="phone_number">Phone Number</string> <string name="password">Password</string> </resources>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sweetlover.edittextdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sweetlover.activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>