计数器

/*计数器*/
mEtExplain.addTextChangedListener(new TextWatcher() {
public int myselectionStart;
public int myselectionEnd;

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
//文本框文字长度
int mynumber = s.length();
myselectionStart = mEtExplain.getSelectionStart();
myselectionEnd = mEtExplain.getSelectionEnd();
//删除多余输入的字(不会显示出来)
if (mynumber > 100) {
s.delete(myselectionStart - 1, myselectionEnd);
mEtExplain.setText(s.toString().trim());
}
if (mynumber < 100 + 1 && mynumber > -1) {
mTvExplainNum.setText(100 - mynumber + "");
}
//设置光标在最后
mEtExplain.setSelection(s.length());
}
});
/////////////////////////////布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="vertical">

<EditText
android:id="@+id/et_explain"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:background="@null"
android:gravity="start"
android:hint="说明"
android:maxLength="100"
android:textColor="@color/c121"
android:textSize="@dimen/fs3" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp">

<TextView
android:id="@+id/tv_explain_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="12dp"
android:layout_marginEnd="12dp"
android:text="100"
android:textColor="@color/c125"
android:textSize="@dimen/fs3" />
</RelativeLayout>
</LinearLayout>

posted on 2019-12-03 13:15  带镐伤的土豆  阅读(102)  评论(0编辑  收藏  举报

导航