android————EditText

来源:互联网 发布:龙城飞将 知乎 编辑:程序博客网 时间:2024/06/06 03:58

首先关于自动提示的效果

<?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/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="AutoCompleteTextView" />    <AutoCompleteTextView android:id="@+id/autotext"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textColor="#000"        />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="MultiAutoCompleteTextView" />     <MultiAutoCompleteTextView          android:id="@+id/mul"         android:layout_width="fill_parent"           android:layout_height="wrap_content"         /></LinearLayout>

public class AutoActivity extends Activity {private AutoCompleteTextView auto;private MultiAutoCompleteTextView mult;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.autoeditext);auto=(AutoCompleteTextView) findViewById(R.id.autotext);mult=(MultiAutoCompleteTextView) findViewById(R.id.mul); String[] autoString=new String[]{"张航章","张航11","张航22","张航33","张航涨44",};ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,autoString);auto.setAdapter(adapter);auto.setTextColor(Color.BLACK);mult.setAdapter(adapter);mult.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//完成对逗号的拆分}}

在编辑框中显示图片的效果

<?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" >    <EditText        android:id="@+id/showimage"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10" >    </EditText>    <Button        android:id="@+id/btn_showimage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>

public class EditTextImage extends Activity{private EditText editText;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.edittextimage);editText=(EditText) findViewById(R.id.showimage);button=(Button) findViewById(R.id.btn_showimage);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {try {int i=1+new Random().nextInt(4);Field field=R.drawable.class.getDeclaredField("face"+i);   int resourceId=Integer.parseInt(field.get(null).toString());   Bitmap bitmap=BitmapFactory.decodeResource(getResources(), resourceId);   ImageSpan imageSpan=new ImageSpan(EditTextImage.this,bitmap);   SpannableString spannableString=new SpannableString("face");   spannableString.setSpan(imageSpan, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   editText.append(spannableString);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}}


还有一种效果验证表单的效果

<?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:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用android:digits属性(数字)"       />    <EditText android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:digits="0123456789"        android:id="@+id/edt_num"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用android:digits属性(字母)"       />    <EditText android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:digits="qwertyuiopasdfghjklmnbvcxz"        />        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用android:inputType属性(数字)"       />    <EditText android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:inputType="number|textCapCharacters"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用android:inputType属性(邮箱)"       />    <EditText android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:inputType="textEmailAddress"        />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用android:mersic属性(浮点)"       />    <EditText android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:numeric="decimal|signed"        />    <Button        android:id="@+id/btn_specail"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>
public class SpecailActivity extends Activity { private Button button;private EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.specail);button=(Button) findViewById(R.id.btn_specail);editText=(EditText) findViewById(R.id.edt_num);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString value=editText.getText().toString();if(value==null||value.trim().equals("")){editText.setError("请输入内容");}}});}}


原创粉丝点击