Android_EditText
<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="horizontal"
tools:context="com.example.edittext.MainActivity" >
<!-- EditText的常用属性
android:hint 当编辑框内容为空时的提示
android:inputType 限制输入的内容
android:digits 限制输入的字符
requestFocus 自动获得焦点-->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapCharacters"
android:hint="输入大写字母"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="单词首字母为大写"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:hint="句子首字母为大写"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:digits="0123456789abcdef"
android:hint="限定数字16进制数"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:hint="自动获得焦点">
<requestFocus/>
</EditText> </LinearLayout>
EditText设置光标颜显示:
需要在布局文件中指定androd:textCursorDrawable,如果需要设置成与字体一样的颜色,改属性设置为“@null”即可,如果需要自定义颜色,需要自定义一个drawable文件,例如:在drawable下窗井my_cursor.xml,内容如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#000080" />
<size android:width="1dp"/> </shape>

然后,设置android:textCursorDrawable="@drawable/my_cursor",光标颜色就可以改变为指定颜色。
EditText文本设置光标位置:
EditText文本设置为右对齐,并设置有提示文字时,光标默认是在左侧显示的,这里解决的方法为创建一个TextView代替提示按钮,设置在Edittext下方,并在代码中通过监听Edittext的状态,来控制Textview的显示与隐藏,进而达到提示文字在右侧,光标也在右侧的效果。
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.eichinn.practice.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right|center_vertical" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="6dp"
android:gravity="right|center_vertical"
android:background="@null"
android:text="Hello World!"/>
</FrameLayout>
</LinearLayout>
java代码如下:
public class MainActivity extends AppCompatActivity {
private EditText et;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.tv);
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (TextUtils.isEmpty(charSequence)) {
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
参考链接:https://www.jianshu.com/p/a7e88cedce1d
Android_EditText的更多相关文章
- Android_EditText 打勾显示输入的密码 --EditText与setTransformationMethod
实现目标: 实现原理: 为CheckBox添加一个监听器事件; 实现的源码: package edu.cquptzx.showPassword; import android.app.Activity ...
- Android_EditText 密码框默认是小圆点 怎么改成其它的(*)?
text.setTransformationMethod(new AsteriskPasswordTransformationMethod()); public class AsteriskPassw ...
- [转]Android学习:EditText的使用方法
EditText是在Android开发中经常被使用到的控件,主要用来获取用户的输入内容. 1.EditText常用属性 EditText继承自TextView,所以EditText也拥 ...
随机推荐
- 【Pyhton Network】使用poll()或select()实现非阻塞传输
通常情况下,socket上的I/O会阻塞.即除非操作结束,否则程序不会照常进行.而以下集中情况需要在非阻塞模式下进行:1. 网络接口在等待数据时是活动的,可以做出相应:2. 在不使用线程或进程的情况下 ...
- IPv6 tutorial – Part 5: Address types and global unicast addresses
https://4sysops.com/archives/ipv6-tutorial-part-5-address-types-and-global-unicast-addresses/ In my ...
- WCF - Consuming WCF Service
WCF services allow other applications to access or consume them. A WCF service can be consumed by ma ...
- java集合类——Stack类
查看java的API文档,Stack继承Vector类. 栈的特点是后进先出. API中Stack自身的方法不多,基本跟栈的特点有关. import java.util.Stack; public c ...
- POI导出数据内存溢出问题
POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果也不是太好.3.8版本的POI新出来了SXSSFWorkbook,可以支持大数据量的操作,只是SXSSFWo ...
- java.util.concurrent.Exchanger应用范例与原理浅析--转载
一.简介 Exchanger是自jdk1.5起开始提供的工具套件,一般用于两个工作线程之间交换数据.在本文中我将采取由浅入深的方式来介绍分析这个工具类.首先我们来看看官方的api文档中的叙述: A ...
- C# 线程更新UI
最方便的用法: private void ViewMsg(string msg) { /* control.Invoke(new SetControlTextDelegate((ct, ...
- 如何在小方框上打对号 小方框内打对勾 word 方框打对勾
在word中做选择时,很多人遇到需要在小方框上打对勾而不知如何做,现将可行的各种方法总结如下: 1:直接找到一个做好的,保存为图片,在需要的时候插入它: 2:插入文本框,然后边框选择为实线,在文本 ...
- [NOIP2002]自由落体
NOIp2002提高组 题目描述 在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1.在地面上有一个小车(长为 L,高为 K,距原点距离为 S1).已知小球下落距离计算 ...
- linux Grant 添加 MySql 用户
Grant 添加 MySql 用户 2009-04-03 14:40 我安装的版本: mysql> select version();+------------+| version() |+ ...