Skip to content

Commit

Permalink
(#2) code 添加TSUserIntroduceInputView
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineFXX committed Oct 20, 2017
1 parent b594e20 commit 5f89160
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 20 deletions.
49 changes: 49 additions & 0 deletions tsui/src/main/java/com/zhiyicx/tsui/utils/TSConvertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,53 @@ public static int px2sp(Context context, float pxValue) {
return (int) (pxValue / fontScale + 0.5f);
}

/**
* 替换 emoji 长度 =1
*
* @param str
* @return
*/
public static int stringLengthDealForEmoji(CharSequence str) {
int emojiLength = emojiStrLength(str);
return (str.length() - emojiLength) + stringEmojiCount(str);
}

/**
* @param str
* @return 一个 emoji 占两个字,通过长度返回个数
*/
public static int stringEmojiCount(CharSequence str) {
int emojiLenght = emojiStrLength(str);
return emojiLenght / 2;
}

/**
* @param str
* @return 字符串中 emoji 字符长度
*/
public static int emojiStrLength(CharSequence str) {
int emojiLenght = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
if (isEmojiCharacter(str.charAt(i))) {
emojiLenght++;
}
}
return emojiLenght;
}

/**
* emoji 所占的长度
* @param emojiNUm emoji 个数
* @return
*/
public static int emojiStrLength(int emojiNUm) {
return 2*emojiNUm;
}

private static boolean isEmojiCharacter(char codePoint) {
return !(codePoint == 0x0 || codePoint == 0x9 || codePoint == 0xA || codePoint == 0xD || codePoint >= 0x20 && codePoint <= 0xD7FF ||
codePoint >= 0xE000 && codePoint <= 0xFFFD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* @Describe 限制输入控件 view
* @Author Jungle68
* @author Jungle68
* @Date 2017/1/12
* @Contact [email protected]
*/
Expand Down Expand Up @@ -134,7 +134,7 @@ public void afterTextChanged(Editable s) {
if (s.length() >= mShowLimitSize) {
mLimitTipStr = "<" + s.length() + ">" + "/" + mLimitMaxSize;
CharSequence chars = TSColorPhrase.from(mLimitTipStr).withSeparator("<>")
.innerColor(ContextCompat.getColor(context, R.color.tsui_config_color_red))
.innerColor(ContextCompat.getColor(context, R.color.tsui_config_color_red_note))
.outerColor(ContextCompat.getColor(context, R.color.tsui_general_for_hint))
.format();
mTvLimitTip.setText(chars);
Expand Down Expand Up @@ -171,10 +171,10 @@ public void setOnSendClickListener(OnSendClickListener onSendClickListener) {
/**
* 设置发送按钮是否显示
*
* @param isVisiable true 显示
* @param isVisible true 显示
*/
public void setSendButtonVisiable(boolean isVisiable) {
if (isVisiable) {
public void setSendButtonVisible(boolean isVisible) {
if (isVisible) {
mBtSend.setVisibility(VISIBLE);
} else {
mBtSend.setVisibility(GONE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package com.zhiyicx.tsui.widget.edittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhiyicx.tsui.R;
import com.zhiyicx.tsui.utils.TSColorPhrase;
import com.zhiyicx.tsui.utils.TSConvertUtils;

import static android.support.annotation.Dimension.SP;

/**
* @author LiuChao
* @describe 用户个人资料,简介编辑框
* @date 2017/1/17
* @contact email:[email protected]
*/

public class TSUserIntroduceInputView extends FrameLayout {
/**
* 提示 格式xx/xx
*/
protected TextView mTvLimitTip;
/**
* 输入EditText
*/
protected EditText mEtContent;
/**
* 最大输入值
*/
private int mLimitMaxSize;
/**
* 当输入值达到 mShowLimitSize 时,显示提示
*/
private int mShowLimitSize;
/**
* 编辑框的hint提示文字
*/
private String mHintContent;
/**
* 编辑框显示最大行数,超过改行数就滚动
*/
private int mShowLines;
/**
* 输入文字的内容
*/
private int mContentGravity;
/**
* 监听输入框输入内容变化
*/
private ContentChangedListener mContentChangedListener;

/**
* 添加格式符号,用户TSColorPhrase
*/
private String mLimitTipStr = "{}/";

/**
* 对外提供 获取输入EditText
*
* @return EditText
*/
public EditText getEtContent() {
return mEtContent;
}

public TSUserIntroduceInputView(Context context) {
super(context);
init(context, null);
}

public TSUserIntroduceInputView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

public TSUserIntroduceInputView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}

private void init(final Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.ts_view_userinfo_introduce_inputview, this);
mTvLimitTip = findViewById(R.id.tv_limit_tip);
mEtContent = findViewById(R.id.et_content);
if (attrs != null) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TSInputLimitView);
mLimitMaxSize = array.getInteger(R.styleable.TSInputLimitView_tsLimitSize, context.getResources().getInteger(R.integer.comment_input_max_size));
mShowLimitSize = array.getInteger(R.styleable.TSInputLimitView_tsShowLimitSize, context.getResources().getInteger
(R.integer.show_comment_input_size));
mHintContent = array.getString(R.styleable.TSInputLimitView_tsHintContent);
// 如果为0就不要设置maxLine了
mShowLines = array.getInteger(R.styleable.TSInputLimitView_tsShowLines, 0);
// 如果为0就不要设置maxLine了
mContentGravity = array.getInteger(R.styleable.TSInputLimitView_tsContent_gravity, Gravity.START);
mEtContent.setGravity(mContentGravity);
if (array.getDimensionPixelSize(R.styleable.TSInputLimitView_tsContent_size, 0) != 0) {
mEtContent.setTextSize(SP, TSConvertUtils.px2dp(getContext(), array.getDimension(R.styleable.TSInputLimitView_tsContent_size, 0)));
}
array.recycle();
} else {
mLimitMaxSize = context.getResources().getInteger(R.integer.comment_input_max_size);
mShowLimitSize = context.getResources().getInteger(R.integer.show_comment_input_size);
mHintContent = context.getResources().getString(R.string.tsui_edit_introduce);
mShowLines = 0;
}

// 初始化控件属性 2*mLimitMaxSize 用于兼容 emoji
mEtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2 * mLimitMaxSize)});
mEtContent.setHint(mHintContent);
if (mShowLines > 0) {
mEtContent.setLines(mShowLines);
}
mTvLimitTip.setVisibility(GONE);
mEtContent.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) {
if (mContentChangedListener != null) {
mContentChangedListener.contentChanged(s);
}
}

@Override
public void afterTextChanged(Editable s) {
// 一下是处理适配 emoji, 让emoji 算成一个长度
int parseContentLength = TSConvertUtils.stringLengthDealForEmoji(s);
mLimitTipStr = "<" + parseContentLength + ">" + "/" + mLimitMaxSize;
int emojiNum = TSConvertUtils.stringEmojiCount(s);
mEtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mLimitMaxSize + emojiNum + 1)});
if (parseContentLength > mLimitMaxSize) {
String shouldShowContent = s.toString().substring(0, s.toString().length() - (parseContentLength - mLimitMaxSize));
int shouldShowEmojiNum = TSConvertUtils.stringEmojiCount(shouldShowContent);
int offset = emojiNum - shouldShowEmojiNum;
if (offset > 0) {
TSConvertUtils.emojiStrLength(offset);
shouldShowContent = shouldShowContent.substring(0, shouldShowContent.length() - offset);
}
mEtContent.setText(shouldShowContent);
mEtContent.setSelection(shouldShowContent.length());
}
if (parseContentLength >= mShowLimitSize) {
CharSequence chars = TSColorPhrase.from(mLimitTipStr).withSeparator("<>")
.innerColor(ContextCompat.getColor(context, R.color.tsui_config_color_red_note))
.outerColor(ContextCompat.getColor(context, R.color.tsui_general_for_hint))
.format();
mTvLimitTip.setText(chars);
mTvLimitTip.setVisibility(VISIBLE);
mEtContent.setPadding(0, 0, 0, context.getResources().getDimensionPixelSize(R.dimen.tsui_default_30));
} else {
mTvLimitTip.setVisibility(GONE);
mEtContent.setPadding(0, 0, 0, 0);
}
}
});
}

/**
* 获取输入内容
*
* @return 当前输入内容,去掉前后空格
*/
public String getInputContent() {
return mEtContent.getText().toString().trim();
}

public void setText(String content) {
mEtContent.setText(content);
}

/**
* 因为控件使用了TextChangedListener,无法在外面再次创建一个监听
* 获取控件,在某些时候需要用到
*/
public void setContentChangedListener(ContentChangedListener contentChangedListener) {
mContentChangedListener = contentChangedListener;
}

public interface ContentChangedListener {
/**
* 内容改变
*
* @param s 内容
*/
void contentChanged(CharSequence s);
}
}
21 changes: 9 additions & 12 deletions tsui/src/main/res/layout/ts_view_input_limit_viewgroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tsui_config_color_white">
android:background="@color/input_view_bg_color">

<View
style="@style/style_divider_horizontal"/>
Expand All @@ -24,8 +24,7 @@
<EditText
android:id="@+id/et_content"
style="@style/style_edit_bg_line"
android:layout_toLeftOf="@+id/tv_limit_tip"
android:layout_toStartOf="@id/tv_limit_tip"
android:layout_toLeftOf="@+id/bt_send"
tools:text="视卡鼓励进口大幅拉升刚刚加息公积金阿哥电视卡鼓励进口大幅拉刚刚加息公积金阿哥电视卡鼓励进口大幅拉刚刚加"
/>

Expand All @@ -42,22 +41,20 @@
android:gravity="center"
android:textColor="@color/tsui_config_color_white"
android:textSize="@dimen/tsui_text_size_14"
android:visibility="gone"/>
android:visibility="gone"
/>

<TextView
android:id="@+id/tv_limit_tip"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/tsui_default_10"
android:layout_above="@id/bt_send"
android:layout_toRightOf="@id/et_content"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingBottom="@dimen/tsui_default_10"
android:textColor="@color/tsui_config_color_black"
android:textSize="@dimen/tsui_text_size_11"
android:visibility="gone"
android:paddingBottom="@dimen/tsui_default_5"
android:textColor="@color/tsui_config_color_red_note"
android:textSize="@dimen/tsui_text_size_10"
tools:text="210/255"/>
</RelativeLayout>
</FrameLayout>
35 changes: 35 additions & 0 deletions tsui/src/main/res/layout/ts_view_userinfo_introduce_inputview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tsui_config_color_white"
>
<!--用户编辑页面,简介编辑的布局-->
<!--简介编辑框-->
<EditText
android:id="@+id/et_content"
style="@style/style_edit_cursor"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tsui_config_color_white"
android:gravity="top|start|left"
android:hint="@string/tsui_edit_introduce"
android:text=""
android:textColor="@color/tsui_general_for_content"
android:textColorHint="@color/tsui_general_for_hint"
android:textSize="@dimen/tsui_text_size_16"/>
<!--字数提示-->
<TextView
android:id="@+id/tv_limit_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/et_content"
android:layout_alignEnd="@id/et_content"
android:layout_alignRight="@id/et_content"
android:gravity="right"
android:textColor="@color/tsui_config_color_red_note"
android:textSize="@dimen/tsui_text_size_14"
tools:text="210/255"/>
</RelativeLayout>
2 changes: 2 additions & 0 deletions tsui/src/main/res/values/tsui_colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<color name="tsui_config_color_blue">#1B88EE</color>
<color name="tsui_config_color_50_blue">#801B88EE</color>
<color name="tsui_config_color_red">#FA3A3A</color>
<color name="tsui_config_color_red_note">#f4504d</color>
<color name="tsui_config_color_separator">#DEE0E2</color>
<color name="tsui_config_color_separator_darken">#D4D6D8</color>
<color name="tsui_config_color_background">#F4F5F7</color>
Expand Down Expand Up @@ -88,6 +89,7 @@
<!--
时间、提示文字、色块类按钮不可点击状态 等次要信息
-->
<color name="tsui_general_for_content">#333333</color>
<color name="tsui_general_for_hint">#cccccc</color>
<color name="tsui_general_for_hint_alpha">#80cccccc</color>
<color name="input_view_bg_color">#fafafa</color>
Expand Down
1 change: 1 addition & 0 deletions tsui/src/main/res/values/tsui_dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<!--**********************************************
* EditText *
**********************************************-->
<dimen name="tsui_text_size_10">10sp</dimen>
<dimen name="tsui_text_size_11">11sp</dimen>
<dimen name="tsui_text_size_13">13sp</dimen>
<dimen name="tsui_text_size_14">14sp</dimen>
Expand Down
Loading

0 comments on commit 5f89160

Please sign in to comment.