赞
踩
图1:没有输入评论内容,(0~300)始终显示在最右边,并且与该行提示内容保持在同一行
图2:输入评论内容时,后面文字数量文本跟着变化(215~300)始终显示在最右边,如果最后一行文本的宽度小于等于指定宽度,后面文 字数量文本和同一行的评论文本保持在同一行
图3:输入评论内容时,后面文字数量文本跟着变化(200~300)始终显示在最右边,如果最后一行文本的宽度 超出 指定宽度,后面文字数量文本显示在最后一行的评论文本右下面
PixelUtils工具类
/** * 获取屏幕的宽度 * * @param context * @return */ public static int getScreenWidth(Context context) { int widthPixels = context.getResources().getDisplayMetrics().widthPixels; return widthPixels; } /** * 将dip或dp值转换为px值,保证尺寸大小不变 * * @param dipValue * @return */ public static int dip2px(float dipValue) { final float scale = App.getAppContext().getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); }
activity_editext布局代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/dp_22"> <ScrollView android:layout_width="match_parent" android:layout_height="@dimen/dp_140" android:layout_marginTop="@dimen/dp_50" android:scrollbars="none"> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/dp_140" android:background="@drawable/shanpe_rect_white_f7_radius4"> <EditText android:id="@+id/edit_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:maxLength="300" android:gravity="top|left" android:hint="客官哪里不满意" android:paddingLeft="@dimen/dp_12" android:paddingTop="@dimen/dp_12" android:paddingRight="@dimen/dp_12" android:textColorHint="@color/warm_grey_9" android:textSize="@dimen/sp_14" /> <TextView android:id="@+id/tv_input_text_count1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/edit_comment" android:layout_alignParentRight="true" android:text="(0~300字)" android:textColor="@color/warm_grey_9" android:textSize="@dimen/sp_13" /> <TextView android:id="@+id/tv_input_text_count2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edit_comment" android:layout_alignParentRight="true" android:text="(0~300字)" android:textColor="@color/warm_grey_9" android:textSize="@dimen/sp_13" android:visibility="gone" /> </RelativeLayout> </ScrollView> </LinearLayout>
Java代码
public class ActivityEditext extends AppCompatActivity { @BindView(R.id.edit_comment) EditText editComment; @BindView(R.id.tv_input_text_count1) TextView tvInputTextCount1; @BindView(R.id.tv_input_text_count2) TextView tvInputTextCount2; private int dp22; private int dp10; private int dp140; private int editextWidth; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_editext); ButterKnife.bind(this); initData(); initEvent(); } private void initData() { dp22 = PixelUtils.dip2px(22); dp10 = PixelUtils.dip2px(10); dp140 = PixelUtils.dip2px(140); int screenWidth = PixelUtils.getScreenWidth(this); editextWidth = screenWidth - (dp22 * 2 + dp10 * 2); } private void initEvent() { // editComment.setMovementMethod(ScrollingMovementMethod.getInstance()); editComment.addTextChangedListener(new TextWatcher() { @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) { /* if (s.length() > 300) { ToastUtils.show("已达字数最大限制"); return; }*/ if (s.length() == 300) {// 布局里添加 android:maxLength="300",大于300时也只会取前300个字符,当内容始终是大于或者等于300个字符时,只回调一次 ToastUtils.show("已达字数最大限制"); } if (s.length() > 0) { tvInputTextCount1.setText("(" + s.length() + "/300字)"); tvInputTextCount2.setText("(" + s.length() + "/300字)"); } else { tvInputTextCount1.setText("(0~300字)"); tvInputTextCount2.setText("(0~300字)"); } Layout layout = editComment.getLayout(); int lineCount = layout.getLineCount(); int editCommentHeight = editComment.getHeight(); float lineWidth = layout.getLineWidth(lineCount - 1);// 获取文本最后一行宽度 int tvInputTextCount1width = 0; if (tvInputTextCount1.getVisibility() == View.VISIBLE) { tvInputTextCount1width = tvInputTextCount1.getWidth(); } else if (tvInputTextCount2.getVisibility() == View.VISIBLE) { tvInputTextCount1width = tvInputTextCount2.getWidth(); } int contentWidth = editextWidth - tvInputTextCount1width; contentWidth = PixelUtils.px2dip(contentWidth); lineWidth = PixelUtils.px2dip(lineWidth); if (lineWidth <= contentWidth) { tvInputTextCount1.setVisibility(View.VISIBLE); tvInputTextCount2.setVisibility(View.GONE); } else if (editCommentHeight > dp140 && lineWidth <= contentWidth) { tvInputTextCount1.setVisibility(View.VISIBLE); tvInputTextCount2.setVisibility(View.GONE); } else { tvInputTextCount1.setVisibility(View.GONE); tvInputTextCount2.setVisibility(View.VISIBLE); } } }); } }
示例核心代码是在afterTextChanged方法里,如果onTextChanged里实现,计算过于频繁。
欢迎关注我的公众号,不定期推送优质的文章,
微信扫一扫下方二维码即可关注。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。