EditText
提供了一个方法
addTextChangedListener
实现对输入文本的监控。
在
addTextChangedListener这个方法中,需要一个TextWatcher对象,在TextWatcher中提供了三个回调方法:(1)文本改变之前:beforTextChanged,(2)文本改变:onTextChanged,(3)文本改变之后:afterTextChange.
布局文件只需要一个是EditText和两个TextView(图中的“5”用到的就是这个TextView,“/500”是另写的静态的文本框)。
布局如下:
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
android:text="/500" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"//去掉边框
android:hint="请简要描述您遇到的问题,我们会尽快为您解决(最少5个字)"
android:layout_alignParentLeft="true"
android:textSize="15sp"
android:gravity="top"
android:cursorVisible="false"//初始化光标不显示
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/textView4"
android:textColor="@android:color/darker_gray"
android:text="0" />
在MainActivity的onCreate方法中添加如下代码:
tv_show = (TextView) findViewById(R.id.textView3);
ed_content = (EditText) findViewById(R.id.editText1);
ed_content.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int editStart;
private int editEnd;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
temp = s;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// tv_show.setText(s);// 将输入的内容实时显示
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
editStart = ed_content.getSelectionStart();
editEnd = ed_content.getSelectionEnd();
tv_show.setText(String.valueOf(temp.length()));//此处需要进行强制类型转换
if (temp.length() > 500) {//条件判断可以实现其他功能
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
ed_content.setText(s);
ed_content.setSelection(tempSelection);
Toast.makeText(MainActivity.this, "你输入的字数已经超过了!", Toast.LENGTH_SHORT).show();
总结:文本框自动计数,主要依靠afterTextChanged方法实现。
效果图如下所示(文本框中输入数字时,右下角自动统计数字)实现思路EditText提供了一个方法addTextChangedListener实现对输入文本的监控。在addTextChangedListener这个方法中,需要一个TextWatcher对象,在TextWatcher中提供了三个回调方法:(1)文本改变之前:beforTextChanged,(2)文本改变:onTextC...
CounterView一个简单的Android计数器视图,用于显示edittext字符计数。
它的行为与我们的BufferTextInputLayout相同,除了它不环绕EditText视图,使您可以将其放置在其他位置。CounterView一个简单的Android计数器视图,用于显示edittext字符计数。
此行为与BufferTextInputLayout相同,不同之处在于它不会环绕EditText视图,而是允许您将其放置在布局中的其他位置。
CounterView将在edittext中显示输入内容的当前计数。
您需要使用以下命令设置允许的最大内容长度:view_counter.counterMaxLength = someNumberValue您可以使用app:counter自定义计数器的颜色
private int num = 100;
//etNoteContent是EditText
etNoteContent.addTextChangedListener(new TextWatcher() {
private CharSequence wordN...
知识点1:android:addStatesFromChildren=”” // 父控件获取子控件状态
知识点2:requestFocus // 使控件获得焦点
知识点3:addTextChangedListener // EditText添加输入监听<!-- 在父Vie
Android EditText显示一个特别大的整数(比如25位),不允许用科学计数法显示。
作为Android外行人,我提出在末尾加入一个Android 无法显示的特殊字符,使得这串特别大的整数无法用科学计数法显示,比如ASCII码中的第14位。测试后,Android 将该字符显示为一个空格,整体效果达到了预期。
public class ImmUtils {
public static void dfFormat(Object object) {
double pi = 3.1415927;//圆周率
//取一位整数 ...
CounterView
A simple Android counter view for showing edittext character counts. This behaves in the same way as our
BufferTextInputLayout except that it does not wrap
around an EditText view, allowing you to place it elsewhere within your layout.
The CounterView will display the current count of the input content in the edittext. You need to set the max content length allowed using:
view_counter.counterMaxLength = someNumberValue
You can customise the color of the counter using the app:counterT
design包已经出来有几年了,其中就有TextInputLayout&TextInputEditText,<android.support.design.widget.TextInputLayout
android:id="@+id/rl_phone"
android:layout_width="match_parent"
android:layout_height="50
您好,关于android studio文本框代码实现,您可以使用EditText控件来实现文本框,具体实现方法可以参考以下代码:
在xml布局文件中添加EditText控件:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入文本内容"/>
在java代码中获取EditText控件,并设置监听器:
EditText editText = findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 文本内容改变时的操作
@Override
public void afterTextChanged(Editable s) {
以上是一个简单的实现方法,您可以根据自己的需求进行修改和扩展。希望能对您有所帮助。
完美解决IDEA无法启动Tomcat显示[localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirector
56574
创建Maven项目,提示'settings.xml' has syntax errors less... (Ctrl+F1) Inspection info: Inspects a Maven...
14053