`
fanfq
  • 浏览: 264115 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

输入框计数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>

 

 

 

  • 大小: 16.8 KB
  • demo.zip (41.7 KB)
  • 描述: resource
  • 下载次数: 11
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics