skywang12345

导航

 

Android控件之EditText

应用实例

新建一个activity,有一个空间edittext和两个button。点击其中的一个button,显示edittext的文本;点击另一个button,删除exittext的最后一个字符。


应用程序代码

 1 package com.skywang.control;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.widget.Toast;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.util.Log;
12 import android.text.TextUtils;
13 import android.text.Spannable;
14 import android.text.Selection;
15 
16 public class EditTextTest extends Activity implements View.OnClickListener{
17     private static final String TAG = "SKYWANG";
18     
19     private EditText mEdit;
20     private Button mBtnShow;
21     private Button mBtnDelete;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.edit_text_test);
26         
27         // 获取edittext
28         mEdit = (EditText)findViewById(R.id.edit_test);
29         
30         // 获取show按钮
31         mBtnShow = (Button)findViewById(R.id.btn_show);
32         // 设置show按钮的监听函数
33         mBtnShow.setOnClickListener(this);
34         
35         // 获取delete按钮
36         mBtnDelete = (Button)findViewById(R.id.btn_delete);
37         // 设置delete按钮的监听函数
38         mBtnDelete.setOnClickListener(this);
39         
40         // 打印log信息
41         Log.d(TAG, "on create");
42     }
43     
44     @Override
45     public void onClick(View v) {
46         switch(v.getId()) {
47             case R.id.btn_show: {
48                 Log.d(TAG, "click button show! text:"+mEdit.getText().toString());
49                 Toast.makeText(getApplicationContext(), mEdit.getText().toString(), Toast.LENGTH_LONG).show();
50                 break;    
51             }
52             case R.id.btn_delete: {
53                 if (!TextUtils.isEmpty(mEdit.getText())) {
54                     String text = mEdit.getText().toString();
55                     int len = text.length() - 1;
56                     mEdit.setText(mEdit.getText().subSequence(0, len));
57                     // 设置光标位置
58                     setEditTextIndicator(mEdit, mEdit.length());
59                 }
60                 
61                 break;    
62             }
63         }
64     }
65     
66     /*
67      * 设置EditText光标位置。
68      * 若输入的光标位置无效,则将光标设置到EditText末尾。
69      * 参数说明:
70      *     et —— 被设置的EditText
71      *     index —— 设置后的光标位置
72      */
73     private void setEditTextIndicator (EditText et, int index) {
74         CharSequence text = et.getText();
75         if (text instanceof Spannable) {
76             Spannable spanText = (Spannable)text;
77             
78             try {
79                 Selection.setSelection(spanText, index);
80             }catch (IndexOutOfBoundsException e) {
81                 Selection.setSelection(spanText, text.length());
82             }
83         }    
84     }
85 }
View Code

layout文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6     
 7     <EditText
 8         android:id="@+id/edit_test"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:textSize="32sp"
12         android:hint="@string/hello_world" />
13     
14     <Button
15         android:id="@+id/btn_show"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:text="@string/text_show" 
19         />
20 
21     <Button
22         android:id="@+id/btn_delete"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:text="@string/text_delete" 
26         />
27     
28 </LinearLayout>
View Code

manifest文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.skywang.control"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.skywang.control.EditTextTest"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26 
27 </manifest>
View Code

程序效果截图

 

 

点击下载:源代码

 

posted on 2013-06-15 09:04  如果天空不死  阅读(751)  评论(0编辑  收藏  举报