Android初级教程_监听EditText输入文字的字数
2012-08-02 11:48 java线程例子 阅读(421) 评论(0) 编辑 收藏 举报功能很简单,直接上代码.
public void init(){ chCounterText = (TextView) findViewById(R.id.sdk_status_ch_counter); statusEdit = (EditText) findViewById(R.id.sdk_status_edit_text); statusEdit.addTextChangedListener(new 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) { String content = statusEdit.getText().toString(); chCounterText.setText(content.length() + "/" + MAX_LENGTH); } }); }
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" > //scrollviwe主要是当输入法启动的时候,让EditText自动自动往上滑.否则输入法可能会把输入框遮住
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/renren_sdk_status_ch_counter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:gravity="right"
android:text="0/20"
android:textColor="#999999"
android:textSize="14sp" />
<EditText
android:id="@+id/renren_sdk_status_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="3dip"
android:autoText="true"
android:capitalize="sentences"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:gravity="top"
android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
android:isScrollContainer="false"
android:maxLength="20"
android:minHeight="24dip"
android:minLines="5"
android:padding="4dip"
android:textSize="16sp" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
</LinearLayout>
欢迎转载:http://blog.csdn.net/johnny901114/article/details/7822255 如有其他问题请留言! 谢谢