5th-安卓操作-EditText
概念区分:TextView是不可编辑文本框,而EditView是可编辑的文本框,作为用户与系统之间的文本输入接口,用户通过这个组件可以把数据传给Android系统,然后得到我们想要的数据。
常用方法:
方法 | 功能描述 | 返回值 |
setImeOptions | 设置软键盘的enter值 | void |
getImeActionLabel | 设置IME动作标签 | CharSequence |
getDefaultEditable | 获取是否默认可编辑 | boolean |
setEllipsize | 设置当文字过长时控件显示的方式 | void |
setMarqueeRepeatLimit | 在ellipsize指定marquee的情况下,设置重复滚动的次数 | void |
setIncludeFontPadding | 设置文本框是否包含底部和顶端的额外空白 | void |
setGravity | 设置文本框在布局中的位置 | void |
getGravity | 获取文本框在布局中的位置 | int |
getFreezesText | 获取保存的文本内容及光标位置 | boolean |
setFreezesText | 设置保存的文本内容及光标位置 | void |
setHint | 设置文本框为空时,文本框默认显示的字符 | void |
getHint | 获取文本框为空时,文本框默认显示的字符 | CharSequence |
下面贴上代码:
java:
1 import android.support.v7.app.ActionBarActivity; 2 import android.os.Bundle; 3 import android.view.Menu; 4 import android.view.MenuItem; 5 6 import android.widget.TextView; 7 import android.widget.EditText; 8 import android.view.KeyEvent; 9 import android.widget.TextView.OnEditorActionListener; 10 11 public class MainActivity extends ActionBarActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 EditText ET_phone=(EditText)findViewById(R.id.ET_phonenumber); 18 EditText ET_password=(EditText)findViewById(R.id.ET_password); 19 20 final TextView text=(TextView)findViewById(R.id.myTextView); 21 ET_phone.setOnEditorActionListener(new OnEditorActionListener(){ 22 public boolean onEditorAction(TextView v,int actionID,KeyEvent event){ 23 text.setText("Editing ET_phonenumber"); 24 return false; 25 } 26 }); 27 ET_password.setOnEditorActionListener(new OnEditorActionListener(){ 28 public boolean onEditorAction(TextView v,int actionID,KeyEvent event){ 29 text.setText("Editing ET_password"); 30 return false; 31 } 32 33 }); 34 } 35 36 @Override 37 public boolean onCreateOptionsMenu(Menu menu) { 38 // Inflate the menu; this adds items to the action bar if it is present. 39 getMenuInflater().inflate(R.menu.main, menu); 40 return true; 41 } 42 43 @Override 44 public boolean onOptionsItemSelected(MenuItem item) { 45 // Handle action bar item clicks here. The action bar will 46 // automatically handle clicks on the Home/Up button, so long 47 // as you specify a parent activity in AndroidManifest.xml. 48 int id = item.getItemId(); 49 if (id == R.id.action_settings) { 50 return true; 51 } 52 return super.onOptionsItemSelected(item); 53 } 54 }
xml代码:
1 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.hello2.MainActivity" > 10 11 <TextView 12 android:id="@+id/myTextView" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content"/> 15 <EditText 16 android:id="@+id/ET_phonenumber" 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:maxLength="40" 20 android:hint="Please enter your phone" 21 android:textColorHint="#FF000000" 22 android:phoneNumber="true" 23 android:imeOptions="actionGo" 24 android:inputType="date"/> 25 26 <EditText 27 android:id="@+id/ET_password" 28 android:layout_width="fill_parent" 29 android:layout_height="wrap_content" 30 android:layout_alignParentLeft="true" 31 android:layout_below="@+id/myTextView" 32 android:layout_marginTop="37dp" 33 android:ems="10" 34 android:hint="Please enter your password" 35 android:imeOptions="actionSearch" 36 android:maxLength="40" 37 android:password="true" 38 android:textColorHint="#FF000000" > 39 40 <requestFocus /> 41 </EditText> 42 43 </RelativeLayout>
效果图: