Android UI控件二
来源:互联网 发布:工作交办源码 编辑:程序博客网 时间:2024/06/03 13:10
Android UI 控件二
一、EditText输入文本控件
除了TextView控件之外的属性,还可以实现输入文本内容
1.输入表情图像
实现案例
public class MainActivity extends Activity {private EditText et1;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et1 = (EditText) findViewById(R.id.eedittext01);btn = (Button) findViewById(R.id.button);btn.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {int randomId = new Random().nextInt(9) + 1;try {Field filed = R.drawable.class.getField("face" + randomId);int resourceId = Integer.parseInt(filed.get(null).toString());// 在android中显示图片信息Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);// 使用图片代替文字ImageSpan imageSpan = new ImageSpan(MainActivity.this,bitmap);SpannableString spannableString = new SpannableString("face");spannableString.setSpan(imageSpan, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);et1.append(spannableString);} catch (Exception e) {}}});}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/eedittext01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加表情" /></LinearLayout>
2.输入特定的字符,实现校验功能
实现案例
public class MainActivity extends Activity {private EditText et1;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);et1 = (EditText) findViewById(R.id.edittext01);btn = (Button) findViewById(R.id.button);btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {String editText = et1.getText().toString();if (editText == null || editText.trim().equals("")) {et1.setError("请输入内容!");}}});}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用digits输入数字" /> <EditText android:id="@+id/edittext01" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:digits="0123456789" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用inputType输入数字" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:inputType="number|textCapCharacters" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用inputType输入邮箱" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:inputType="textEmailAddress" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用numeric输入小数" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:numeric="decimal" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定" /></LinearLayout>
二、AutoCompleteTextView自动完成输入内容的控件
1.城市名称的模糊查询
实现案例
public class MainActivity extends Activity {private AutoCompleteTextView auto;private MultiAutoCompleteTextView mul;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);auto = (AutoCompleteTextView) findViewById(R.id.autocompletetextview01);String[] city = new String[] { "浙江温州", "浙江杭州", "浙江宁波", "福建厦门", "福建福州" };ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line,city);auto.setDropDownBackgroundResource(R.color.black);auto.setTextColor(Color.BLACK);auto.setThreshold(1);auto.setAdapter(adapter);mul = (MultiAutoCompleteTextView) findViewById(R.id.multiautocompletetextview01);mul.setDropDownBackgroundResource(R.color.black);mul.setTextColor(Color.BLACK);mul.setThreshold(1);mul.setAdapter(adapter);// 完成对选项的拆分功能,以逗号进行拆分mul.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="AutoCompleteTextView" /> <AutoCompleteTextView android:id="@+id/autocompletetextview01" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MultiAutoCompleteTextView" /> <MultiAutoCompleteTextView android:id="@+id/multiautocompletetextview01" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
三、Button普通按钮控件
Button控件的基本用法和TextView、EditText相似,最常用的是按钮单击事件
1.按钮的焦点变化
实现案例
public class MainActivity extends Activity implements OnClickListener,OnTouchListener, OnFocusChangeListener, OnKeyListener {private Button commonBtn, imageBtn;private int value = 1;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);commonBtn = (Button) findViewById(R.id.button01);imageBtn = (Button) findViewById(R.id.button02);commonBtn.setOnClickListener(this);imageBtn.setOnClickListener(this);imageBtn.setOnTouchListener(this);imageBtn.setOnFocusChangeListener(this);imageBtn.setOnKeyListener(this);}public void onClick(View v) {Button button = (Button) v;if (value == 1&& button.getWidth() == getWindowManager().getDefaultDisplay().getWidth()) {value = -1;} else if (value == -1 && button.getWidth() < 50) {value = 1;}button.setWidth(button.getWidth() + (int) (button.getWidth() * 0.1)* value);button.setHeight(button.getHeight() + (int) (button.getHeight() * 0.1)* value);}public boolean onKey(View v, int keyCode, KeyEvent event) {if (KeyEvent.ACTION_DOWN == event.getAction()) {v.setBackgroundResource(R.drawable.button2);} else if (KeyEvent.ACTION_UP == event.getAction()) {v.setBackgroundResource(R.drawable.button3);}return false;}public void onFocusChange(View v, boolean hasFocus) {if (hasFocus) {imageBtn.setBackgroundResource(R.drawable.button4);} else {imageBtn.setBackgroundResource(R.drawable.button1);}}public boolean onTouch(View v, MotionEvent event) {if (MotionEvent.ACTION_UP == event.getAction()) {v.setBackgroundResource(R.drawable.button1);} else if (MotionEvent.ACTION_DOWN == event.getAction()) {v.setBackgroundResource(R.drawable.button2);}return false;}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的按钮一" /> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button1" android:gravity="center" android:text="按钮" /></LinearLayout>
2.图文混排的按钮
实现案例
public class MainActivity extends Activity {private Button btn;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn = (Button) findViewById(R.id.button);SpannableString spannableStringLeft = new SpannableString("left");Bitmap bitmapLeft = BitmapFactory.decodeResource(getResources(),R.drawable.left_1);ImageSpan imageSpanLeft = new ImageSpan(bitmapLeft);spannableStringLeft.setSpan(imageSpanLeft, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);SpannableString spannableStringRight = new SpannableString("Right");Bitmap bitmapRight = BitmapFactory.decodeResource(getResources(),R.drawable.right_2);ImageSpan imageSpanRight = new ImageSpan(bitmapRight);spannableStringRight.setSpan(imageSpanRight, 0, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);btn.append(spannableStringLeft);btn.append("我的按钮");btn.append(spannableStringRight);}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="120dp" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/b2" android:text="按钮一" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="30dp" android:drawableTop="@drawable/b2" android:text="按钮二" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/b2" android:drawableTop="@drawable/b2" android:text="按钮三" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="30dp" android:drawableRight="@drawable/b2" android:text="按钮三" /> </LinearLayout> <Button android:id="@+id/button" android:layout_width="200dp" android:layout_height="200dp" android:layout_marginTop="10dp" /></LinearLayout>
- Android UI控件二
- 【Android基础】(8)UI控件(二)
- Android UI布局与控件(二)
- Android UI控件学习笔记(二)
- android UI进阶之自定义组合控件二
- Android UI控件组合应用之二:按钮布局
- Android之UI学习篇十二:ListView控件学习(二)
- Android View measure (二) 自定义UI控件measure相关
- Android UI 设计:ListView控件和ArrayAdapter适配器(二)
- Android Material Design新UI控件使用大全 二
- Android Material Design新UI控件使用大全 二
- Android Material Design新UI控件使用大全 二
- Android Material Design新UI控件使用大全 二
- Android示例-JavaScript与UI控件交互(二)
- android UI控件学习
- Android UI控件一
- Android UI控件三
- Android UI控件四
- 4款好用的Android设备HTML编辑器
- Nutch源代码研究 网页抓取 下载插件
- word 目录里面添加分节符下一页出现空白页的解决方法
- iphone jabber XMPP im
- IOS下载资源zip到本地然后读取
- Android UI控件二
- tcp/ip详解卷1:协议 读书笔记 第1章 概述
- java的动态代理机制
- KB2847204在vmware workstation 9.0.2虚拟机上出现白屏
- Android开发必看知识,不看后悔
- 文件批量改名工具v2.0_绿色免安装版
- linux中request_region()函数的分析
- 15.5
- 在线机器学习算法及其伪代码