您的位置:首页 > 移动开发 > Android开发

android源代码提示文本框还能输入多少个字符

2014-08-10 10:05 375 查看
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;
}
});
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐