Android02--Android之UI控件TextView+Button+EditText
2016-08-24 00:38
706 查看
TextView
TextView相关属性,详见API或者http://jingyan.baidu.com/article/c85b7a640641dc003aac9562.html
<TextView android:id="@+id/main_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:textSize="24sp" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:text="mingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbaomingbao" />
TextView实现跑马灯
一个屏幕默认只能显示一个跑马灯,即只会让 android:focusable=”true”
android:focusableInTouchMode=”true”一个textView中的有效.如果要显示多个跑马灯,可以在代码中硬性控制
mText.setSelected(true);.
实现跑马灯的四个控件:
android:ellipsize="marquee" // 可获得焦点 android:focusable="true" // 触摸获得焦点 android:focusableInTouchMode="true" android:singleLine="true"
或者
android:singleLine="true" android:ellipsize="marquee" // 在代码中设置 // 包含上面focusableInTouchMode和focusable这两个属性 mText.setSelected(true);
Button
Button是TextView的子类,增加相应的构造方法,没有差别 下面来两种定义都可以描述一个Button <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click" /> <TextView style="?attr/buttonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Click" />
EditText
EditText也是TextView的一个子控件,具有TextView的所有属性,没个EditText一般都会有自己的inputType.
比较重要的属性:
在单行模式下测试:(因为imeOptions只能对没有用的键进行重定义)
// 单行显示,在此情况下,回车键是废的 // android:singleLine="true" // inputType的属性,默认情况下都是单行模式,可以替代掉singleline. android:inputType="text" // 重新定义回车键的样式 android:imeOptions="actionGo" // 系统默认的样式 /* actionNext 软键盘不关闭,光标跳到下一个输入焦点位置(发送EditorInfo.IME_ACTION_NEXT命令) actionGo 软键盘不关闭,光标跳到下一个输入焦点位置,发送EditorInfo.IME_ACTION_GO命令 actionSearch 软键盘不关闭,发送EditorInfo.IME_ACTION_SEARCH命令 actionSend 软键盘关闭,发送EditorInfo.IME_ACTION_SEND命令 actionDone 软键盘关闭,光标保持在原来的输入框上,发送EditorInfo.IME_ACTION_DONE命令*/
自定义 键的标签
// 在自定义标签中一般都要加上一个actionId,值自己指定(个人理解:因为没个EditText都会有自己的一个ID对应自己系统的imeOptions) android:imeActionId="1" // 上面设置的键盘操作,的按钮文字都是系统默认设置,我们可以通过android:imeActionLabel属性来设置我们自定义的标签文本, android:imeActionLabel="发送"
对回车键的监听:
edit.setOnEditorActionListener(this); // 要实现的方法 // 除了setOnClickListener,一般都会有一个返回值,判断是否完成 // textView表示哪个控件 // actionId:一个EditText只能有一个,表示控件的哪个ID // keyEvent:点击的哪个键出发的事件 @Override public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { switch (textView.getId()) { case R.id.edit_text1: switch (actionId) { case EditorInfo.IME_ACTION_SEND: Toast.makeText(MainActivity.this, "发送", Toast.LENGTH_SHORT).show(); break; break; } break; } return true; }
EditText中常用的监听还有一个:
// 对出入内容的一个控制 edit.addTextChangedListener(this); // 要实现的方法 @Override // start:字符开始的位置 // count:一般都是0;(count表示输入的状态插入式0,替换式替换的字符的大小) // after:一次性向输入框中输入的字符的个数 public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d(TAG, "beforeTextChanged: " + s + "," + start + "," + count + "," + after); } @Override // before: public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d(TAG, "onTextChanged: " + s + "," + start + "," + before + "," + count); } @Override // 点击之后,一般会对文本做一个检测 public void afterTextChanged(Editable s) { Log.d(TAG, "afterTextChanged: " + s); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != 'a') { s.delete(i, i+1); } } }
稍微提一下:
AutoCompleteTextView
AutoCompleteTextView自动补充输入文本,是EditText的一个子类,当我们输入一部分字符时,就会给我们列出和我们输入相匹配的内容。
提供参考:
http://blog.csdn.net/wangfayinn/article/details/8080740
http://android.tgbus.com/Android/tutorial/201108/361860.shtml
http://www.cnblogs.com/zyw-205520/archive/2013/02/15/2912865.html
相关文章推荐
- Android02--Android之UI控件ImageView+ImageButton
- Android学习-常见的UI控件 TextView、EditText和ImageView
- android_常用UI控件_02_EditText_01添加图片到edittext中
- Android基本常用控件的介绍TextView EditText Button Menu
- android UI - 简单控件textView Button ImageView ImageButton
- Android 开发基础篇——UI——基础控件(TextView、Button、EditText)
- Android常见控件之TextView、EditView、Button、Menu
- Android 第五课 常用控件的使用方法(TextView、Button、EditView、 ImageView、 ProgressBar、 ProgressDialog等)
- 【Android UI控件】EditText属性大全,TextView属性大全
- Android常见控件之TextView、EditView、Button、Menu
- Android常用控件-TextView、EditView、Button、Menu
- Android - VGA,ADB,DDMS,安卓开发环境目录结构,安卓目录结构,常见布局,点击事件的几种写法,控件(Button/TextView/ImageView/EditText)
- Android下Button/TextView/EditText/Menu的使用
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- 常见控件的使用方法 TextView EditText Button Menu
- Android - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- Android:UI控件AutoCompleteTextView、MultiAutoCompleteTextView、include、merge、Tokenizer
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, Ra
- Android基础学习 - 简单控件,EditText 、 Button :
- Android 控件之TextView、EditView、CheckBox