《Android开发卷——实时监听文本框输入》

来源:互联网 发布:童话二分之一网络结局 编辑:程序博客网 时间:2024/06/08 15:28

  

在实际开发中,有时候会让用户发布一些类似微博、说说的东西,但是这个是有限制长度的,除了在文本输入框限制长度外,还要在旁边有一条提示还能输入多少个字的“友好提示”。

1、文本框限制输入长度

2、安卓没有提供文本域,这里顺便提一下如果把textview制作成文本域。

<EditText            android:id="@+id/push_edit"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#FFFFFF"            android:hint="请输入你要发表的说说内容"            android:maxLength="100"            android:maxLines="6"            android:minLines="6"            android:textColor="#6C6C6C"            android:textSize="12dp" >            <requestFocus />        </EditText>

监听代码

public class MainActivity extends Activity {private EditText pushEdit;private TextView textNum;private Button btnSave,btnDelete;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pushEdit = (EditText)findViewById(R.id.push_edit);textNum = (TextView)findViewById(R.id.talk_personal_draftbox_num);btnSave = (Button)findViewById(R.id.push_ok);btnDelete = (Button)findViewById(R.id.push_delete);pushEdit.addTextChangedListener(textChange);btnSave.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getBaseContext(), "您提交的内容是:"+pushEdit.getText().toString(), Toast.LENGTH_LONG).show();}});btnDelete.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {pushEdit.setText("");}});}TextWatcher textChange = new TextWatcher(){@Overridepublic void afterTextChanged(Editable s) {if(pushEdit.length()<=100){textNum.setText(100-pushEdit.length()+"");}}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}};}

项目源码:http://download.csdn.net/detail/chillax_li/6920963

尊重原创,转载请注明出处:http://blog.csdn.net/chillax_li/article/details/19177391

0 0
原创粉丝点击