阅读《Android 从入门到精通》(8)——编辑框

来源:互联网 发布:嵌入式软件测试工具 编辑:程序博客网 时间:2024/05/16 01:12

编辑框(EditText)

EditText 类是 TextView 的子类,同时 EditText 类又派生出两个子类 AutoCompleteTextView 和 ExtractEditText。层次关系如下:

java.lang.Object;
android.view.View;
android.widget.TextView;
android.widget.EditText;
AutoCompleteTextView 和 ExtractEditText

EditText 类方法

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();
以上方法均是自身方法,下面给出其他常用方法


EditText 布局



EditText 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9385096
下述程序,主要作用是了解 EditText 的用法。
重点注意:EditText 中的 OnEditorAction 需要注意只有在输入 Enter 后才会被触发!

1.MainActivity.java

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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {// TODO Auto-generated method stubtext.setText("Editing phone number");return false;}});editPassword.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {// TODO Auto-generated method stubtext.setText("Editing password");return false;}});}}

2.activity_main.xml

<?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>

3.color.xml

<?xml version="1.0" encoding="utf-8"?><resources>        <color name="BLACK">#FF000000</color>    </resources>

4.string.xml

<resources>    <string name="app_name">EditTextDemo</string>    <string name="phone_number">Phone Number</string>    <string name="password">Password</string></resources>

5.AndroidManifest.xml

<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>

0 0
原创粉丝点击