您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > Android软件开发5-6
Android软件开发Android软件开发之EditText详解(五)EditText在API中的结构java.lang.Objectandroid.view.Viewandroid.widget.TextViewandroid.widget.EditText已知直接子类:AutoCompleteTextView,ExtractEditText已知间接子类:MultiAutoCompleteTextViewEditText是TextView的直接子类所以EditText会继承父类TextView的一些方法。下面我用自己写的一个Demo和大家详细的说明一下EditView的使用方法。1.简单的EditText输入框非常简单,在layout布局中配置一下EditText在配置一个Button在代码中监听Button的事件获取当前EditView中输入的内容并且显示出来。?xmlversion=1.0encoding=utf-8?LinearLayoutxmlns:android=:id=@+id/textviewllandroid:orientation=verticalandroid:layout_width=fill_parentandroid:layout_height=fill_parentEditTextandroid:id=@+id/sample_edit_text0android:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=简单的EditText输入框/Buttonandroid:id=@+id/sample_button0android:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=确定//LinearLayoutpublicclassSampleActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){setContentView(R.layout.sample);finalEditTexteditText0=(EditText)findViewById(R.id.sample_edit_text0);Buttonbutton0=(Button)findViewById(R.id.sample_button0);button0.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){Stringstr=editText0.getText().toString();Toast.makeText(SampleActivity.this,str,Toast.LENGTH_LONG).show();}});super.onCreate(savedInstanceState);}2.限制EditText输入框的内容在layout中配置信息android:digits=1234567890.+-*/%\n()限制输入框中只能输入自己定义的这些字符串如果输入其它将不予以显示android:phoneNumber=true限制输入框中只能输入手机号码android:password=true限制输入框中输入的任何内容将以*符号来显示android:hint=默认文字输入内容前默认显示在输入框中的文字android:textColorHint=#FF0000设置文字内容颜色android:enabled=false设置输入框不能被编辑3.编辑框中显示图片上一篇讲TextView中就讲过在TextView中添加图片的方法,因为EditText是TextView的子类,所以当然也可以添加图片了,只是一旦在EditText中添加图片以后是不能删除的,如图所示我可以编辑图片旁边的内容,写入文字。?xmlversion=1.0encoding=utf-8?RelativeLayoutxmlns:android=:id=@+id/textviewllandroid:orientation=verticalandroid:layout_width=fill_parentandroid:layout_height=fill_parentEditTextandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=在图片下方android:textColor=#FF0000android:drawableBottom=@drawable/jayandroid:layout_alignParentTop=trueandroid:layout_centerHorizontal=true/EditTextEditTextandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=在图片上方android:textColor=#FF0000android:drawableTop=@drawable/jayandroid:layout_alignParentBottom=trueandroid:layout_centerHorizontal=true/EditTextEditTextandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=在图片左边android:textColor=#FF0000android:drawableLeft=@drawable/jayandroid:layout_alignParentLeft=trueandroid:layout_centerVertical=true/EditTextEditTextandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=在图片右边android:textColor=#FF0000android:drawableRight=@drawable/jayandroid:layout_alignParentRight=trueandroid:layout_centerVertical=true/EditText/RelativeLayout4.设置软键盘的Enter键如图所示我们可以修改软键盘的Enter按钮的样式,可以在代码中监听按钮点击事件。packagecn.m15.xys;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.KeyEvent;importandroid.view.inputmethod.EditorInfo;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;importandroid.widget.TextView.OnEditorActionListener;publicclassKeyBoardActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){setContentView(R.layout.keyboard);EditTexteditText0=(EditText)findViewById(R.id.txtTest0);editText0.setOnEditorActionListener(newOnEditorActionListener(){@OverridepublicbooleanonEditorAction(TextViewarg0,intarg1,KeyEventarg2){if(arg1==EditorInfo.IME_ACTION_GO){Toast.makeText(KeyBoardActivity.this,你点了软键盘'去往'按钮,Toast.LENGTH_SHORT).show();}returnfalse;}});EditTexteditText1=(EditText)findViewById(R.id.txtTest1);editText1.setOnEditorActionListener(newOnEditorActionListener(){@OverridepublicbooleanonEditorAction(TextViewarg0,intarg1,KeyEventarg2){if(arg1==EditorInfo.IME_ACTION_SEARCH){Toast.makeText(KeyBoardActivity.this,你点了软键盘'搜索'按钮,Toast.LENGTH_SHORT).show();}returnfalse;}});EditTexteditText2=(EditText)findViewById(R.id.txtTest2);editText2.setOnEditorActionListener(newOnEditorActionListener(){@OverridepublicbooleanonEditorAction(TextViewarg0,intarg1,KeyEventarg2){if(arg1==EditorInfo.IME_ACTION_SEND){Toast.makeText(KeyBoardActivity.this,你点了软键盘'发送'按钮,Toast.LENGTH_SHORT).show();}returnfalse;}});EditTexteditText3=(EditText)findViewById(R.id.txtTest3);editText3.setOnEditorActionListener(newOnEditorActionListener(){@OverridepublicbooleanonEditorAction(TextViewarg0,intarg1,KeyEventarg2){if(arg1==EditorInfo.IME_ACTION_NEXT){Toast.makeText(KeyBoardActivity.this,你点了软键盘'下一个'按钮,Toast.LENGTH_SHORT).show();}returnfalse;}});EditTexteditText4=(EditText)findViewById(R.id.txtTest4);editText4.setOnEditorActionListener(newOnEditorActionListener(){@OverridepublicbooleanonEditorAction(TextViewarg0,intarg1,KeyEventarg2){if(arg1==EditorInfo.IME_ACTION_DONE){Toast.makeText(KeyBoardActivity.this,你点了软键盘'完成'按钮,
本文标题:Android软件开发5-6
链接地址:https://www.777doc.com/doc-3375726 .html