- public class TestAndroidActivity extends Activity {
- /** Called when the activity is first created. */
- Button clearbtn;
- EditText et;
- TextView tv;
- final int MAX_LENGTH = 50;
- int Rest_Length = MAX_LENGTH;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //tv =(TextView)findViewById(R.);
- tv=(TextView) this.findViewById(R.id.btn);
- //tv=this.findViewById(R.)
- et = (EditText) this.findViewById(R.id.bg);
- et.addTextChangedListener(new TextWatcher() {
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- if(Rest_Length>0){
- Rest_Length = MAX_LENGTH - et.getText().length();
- }
- }
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- tv.setText("还可以输入"+Rest_Length+"个字");
- }
- public void afterTextChanged(Editable s) {
- tv.setText("还可以输入"+Rest_Length+"个字");
- }
- });
- clearbtn = (Button)findViewById(R.id.btn);
- clearbtn.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- et.setText("");
- Rest_Length = MAX_LENGTH;
- }
- });
- }
- }