package com.demo;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
private TextView textView = null;
private EditText editText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
textView.setText("备注(0/100)");
editText.addTextChangedListener(new TextChanedLintener());
}
class TextChanedLintener implements TextWatcher{
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int length = editText.getText().length();
textView.setText("备注("+length+"/100)");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="备注"
/>
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:hint="请在这里填写备注信息,最多可以输入100个字符,汉字按一个字符计算。"
android:gravity="top"
android:textSize="14sp"
android:maxLength="100"
/>
</LinearLayout>

本文介绍了一个简单的Android应用示例,展示了如何使用EditText组件输入文本,并实时更新已输入的字符数,帮助用户了解输入状态。此应用适用于Android开发初学者学习。
646

被折叠的 条评论
为什么被折叠?



