最近毕业设计也快做完了,因为也是边学Android边做毕设,而且也因为是初学,所以用了比较长时间,现在也是希望记下这段时间写Android的一些技巧方法或者是问题。

首先是关于EditText这个控件,这个控件用的也是非常普遍的,毕竟是程序用于和用户进行交互的一个重要控件。

1.取消EditText自动获取焦点的默认行为
一般在一进入有EditText的界面时,EditText就会自动获取焦点,但有时候我们并不希望EditText这种默认行为。

在网上搜了下,发现这种方法是有效的:
在EditText的父控件中加入这段代码:

android:focusable="true";
android:focusableInTouchMode="true";

这样就可以让EditText不会自动获取焦点了。
完整的xml代码如下:

 <LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:focusable="true"android:focusableInTouchMode="true" ><EditText
            android:id="@+id/edit"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="搜索内容"android:singleLine="true"android:textColor="@android:color/black"android:textSize="20sp" /><Button
            android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="搜索"android:textSize="20sp" /></LinearLayout>

2.Eclipse 在xml布局文件中,一旦采用了EditText控件后,可视化视图中就会出现
Exception raised during rendering:java.lang.System.arraycopy([CI[CII)V

Exception details are logged in Window > Show View > Error Log

这个问题,并且也无法看到布局,只能在模拟器上看,但每次仅仅修改下布局就要启动模拟器来查看布局,会非常不方便,于是也上网查了下,发现其实只要修改下不同的API即可,如下图:

只要修改成其他API就可以显示。

3.关于AlertDialog中自定义布局带有的EditText无法弹出键盘
这个之前总结过了,Android学习问题:关于AlertDialog中自定义布局带有的EditText无法弹出键盘

4.关于选择部分或全选文本,以及光标位置问题

(1)选择部分文本和全选:

EditText txt = (EditText) findViewById(R.id.edit);txt.setText("hello!");//txt.setSelection(0, 3);//此方法等同于下面Selection类的方法Selection.setSelection(txt.getEditableText(), 0,3);//全选txt.selectAll();

(2).光标位置的设置

//设置光标位置在最后的位置
txt.setSelection(txt.length());
//Selection.setSelection(etSelection.getEditableText(), 3);//设置光标在第三个字符后面

所以这里分别用了两种方式,一种是直接通过EditText的setSelection方法,另一种则是采用Selection类的setSelection的方法,这两种方法的具体定义如下:

void android.widget.EditText.setSelection(int start, int stop)
public void setSelection (int start, int stop) void android.text.Selection.setSelection(Spannable text, int start, int stop)
public static void setSelection (Spannable text, int start, int stop) 

start:表示选择文本开始的位置;
stop:表示选择文本结束的位置,实际上选择的文本数就是stop-start,也可以说是索引值从start—(stop-1)的范围。

而两种方式的setSelection方法在只有一个int索引值时就是表示设置光标的位置,其int参数就是光标偏离值。

5.对EditText控件内容的监听方法:

EditText txt = (EditText) findViewById(R.id.edit);
txt.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stub}@Overridepublic void afterTextChanged(Editable s) {// TODO Auto-generated method stub}});

还有就是对搜索框内容的清除,参考自Android开发中的一个小功能 清空搜索框的文字

暂时总结到这,未完待续…

Android控件用法总结之EditText相关推荐

  1. android edittext控件常用属性,Android_Android EditText常用属性功能汇总,本文总结分析了Android EditText常 - phpStudy...

    Android EditText常用属性功能汇总 本文总结分析了Android EditText常用属性.分享给大家供大家参考,具体如下: android:hint="请输入数字!" ...

  2. 【Android】关于Android控件EditText的属性InputType的一些经验

    关于Android控件EditText的属性InputType的一些经验   来源:http://blog.163.com/inflexible_simple/blog/static/16769468 ...

  3. android include 控件详解,Android开发中include控件用法分析

    本文实例讲述了Android开发中include控件用法.分享给大家供大家参考,具体如下: 我们知道,基于Android系统的应用程序的开发,界面设计是非常重要的,它关系着用户体验的好坏.一个好的界面 ...

  4. android控件属性

    android控件属性 控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或false an ...

  5. 【Android控件属性记录】

    #Android 控件属性记录 方便查找 控件属性: android属性 android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true ...

  6. android 代码控件框高,Android控件_TextView(显示文本框控件)

    一.TextView控件的常用属性 1.android:id--控件的id 2.android:layout_width--设置控件的宽度 wrap_content(包裹实际文本内容) fill_pa ...

  7. android 按钮吐司,Android控件系列之Toast使用介绍

    Toast英文含义是吐司,在Android中,它就像烘烤机里做好的吐司弹出来,并持续一小段时间后慢慢消失 Toast也是一个容器,可以包含各种View,并承载着它们显示. 使用场景: 1.需要提示用户 ...

  8. 安卓APP_ 控件(3)—— EditText

    摘自:安卓APP_ 控件(3)-- EditText 作者:丶PURSUING 发布时间: 2021-03-29 18:43:40 网址:https://blog.csdn.net/weixin_44 ...

  9. android上传文件用哪个布局,每周总结20130821——android控件的尺寸、http文件上传...

    Android控件的尺寸 android开发中,可以通过编写XML格式的布局文件来实现布局,也可以用纯代码进行布局,通常都是选择XML文件布局.在XML布局文件中,与控件的尺寸有关的属性有androi ...

最新文章

  1. [转]如果我有jQuery背景,我应该如何切换到AngularJS的思维模式?
  2. SAP PM模块实施难点与重点
  3. 提高网站访问速度的34条军规(2)
  4. 分割点云数据_3D点云深度学习综述:三维形状分类、目标检测与跟踪、点云分割等...
  5. stm32 web 参数_老司机们都是怎么学习STM32的?
  6. python命令行输入参数_Python3.x那些事儿:[3]命令行参数传递
  7. 代码审计之SQL注入:BlueCMSv1.6 sp1
  8. VC嵌入python时debug版lib下载
  9. eeg数据集_运动想象,情绪识别等公开数据集汇总
  10. 【TensorFlow】TensorFlow函数精讲之tf.contrib.layers.l1regularizer()-12_regularizer(lambda)
  11. VO(DTO)模式在架构设计中是否需要
  12. pandas 空字符串与na区别_pandas中对nan空值的判断
  13. 《TCP/IP Sockets编程(C语言实现) (第2版)》 代码下载(链接以及文件打包)
  14. 怎样把ACCESS导入SQL数据库
  15. Struts2体系介绍
  16. LightGBM-GBDT-LR使用树集合进行特征转换
  17. 读文献--《机器学习隐私保护研究综述》
  18. LeetCode 刷题记录模板
  19. 视频快搜新片热剧,百度手机浏览器上都有啊
  20. [转]稳定排序和不稳定排序

热门文章

  1. php获取访问者ip地址汇总,php获取访问者IP地址汇总_PHP
  2. mysql取消mvvc机制_MySQL探秘(六):InnoDB一致性非锁定读
  3. eclipse python插件_pydev插件下载-eclipse中的python插件下载6.0.0 官网最新版-西西软件下载...
  4. android socket 长连接_TCP/IP,http,socket,长连接,短连接
  5. boot界面上下键调节键不能动_为什么电脑一开机就自动进入BIOS界面
  6. cronschedulebuilder 到时还没运行完_为什么我的软件编译时没问题,运行时却出错?...
  7. 小程序 遮罩层(阻止事件穿透)
  8. perror的特殊输出
  9. 解决uni-app官方弹框popup关闭不了问题;/pages/extUI/popup/popup;uni-app弹框popup打开调用事件。unin-app弹框封装;
  10. Taro+react开发(24)--this.state和this.props