<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的更多相关文章

  1. Android_EditText 打勾显示输入的密码 --EditText与setTransformationMethod

    实现目标: 实现原理: 为CheckBox添加一个监听器事件; 实现的源码: package edu.cquptzx.showPassword; import android.app.Activity ...

  2. Android_EditText 密码框默认是小圆点 怎么改成其它的(*)?

    text.setTransformationMethod(new AsteriskPasswordTransformationMethod()); public class AsteriskPassw ...

  3. [转]Android学习:EditText的使用方法

        EditText是在Android开发中经常被使用到的控件,主要用来获取用户的输入内容.   1.EditText常用属性   EditText继承自TextView,所以EditText也拥 ...

随机推荐

  1. 函数page_cur_search_with_match

    /****************************************************************//** Searches the right position fo ...

  2. SharePoint 2010在win7 x64 安装

    转:http://kaneboy.blog.51cto.com/1308893/328000 关于<SharePoint 2010应用程序开发指南>,我和杜伟同学正在撰写中,希望下半年早点 ...

  3. Chrome已原生支持“Chrome To Mobile”

    完成PC和手机端Chrome的同gmail帐号绑定后,即可按如下操作进行: 已知在版本“19.0.1084.15”中,这个功能默认未开启,需要进入“chrome://flags/”进行手工启用(早几期 ...

  4. CLR C++ Set Word CustomDocumentProperties

    // WordIssue.cpp : main project file. #include "stdafx.h" using namespace System; using na ...

  5. Android 应用启动渐变效果

    /** * 应用程序启动类:显示欢迎界面并跳转到主界面 * @author liux (http://my.oschina.net/liux) * @version 1.0 * @created 20 ...

  6. ruby编程语言-学习笔记2(第4章 表达式和操作符)

    对属性和数组元素的赋值 o.m + = 1  ===>o.m = (o.m()+ 1)  # 对 o.m()这个方法的结果+1 对数组元素的赋值也是通过 方法调用来完成. 0.[] = 1 == ...

  7. Logo常用的12种颜色

    1.浅绿 #8cc540 2. 深绿 #009f5d 3. 暗蓝 #2456ab 4. 蓝色 #019fde 5. 深蓝 #007cdc 6. 深紫 #887ddd 7. 浅紫 #cd7bdd 8. ...

  8. [codevs2152]滑雪

    题目来源 http://www.tyvj.cn/p/1004 http://www.luogu.org/problem/show?pid=1434# http://codevs.cn/problem/ ...

  9. nyoj重建二叉树(不真的建立)

      感觉c++很陌生啊 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数据(少于100组),以文件结尾结束.每组数据仅一行,包括两个字符串,中间用 ...

  10. Esper系列(四)Output

    功能 控制Esper事件流计算结果的输入形式.时间点及频率: 格式 1  ";