输入框计数Demo

 

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>

 
输入框计数Demo

 

 

你可能感兴趣的:(android,xml,OS)