实现效果图:
知识点:
知识点1:android:addStatesFromChildren=”” // 父控件获取子控件状态
知识点2:requestFocus // 使控件获得焦点
知识点3:addTextChangedListener // EditText添加输入监听
知识点4:android:fillViewport // ScrollView充满
<!-- 在父View中添加android:addStatesFromChildren="true",设置背景选择器,使Layout可以获取子View的焦点情况,改变背景 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:addStatesFromChildren="true"
android:background="@drawable/bg_edittext_selector"
android:orientation="vertical" >
<EditText
android:id="@+id/feedback_et"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="@null"
android:gravity="top|left"
android:maxLength="400"
android:hint="@string/feedback_ed_hint"
android:padding="5dp"
android:textSize="@dimen/title_txt_size" >
</EditText>
<TextView
android:id="@+id/feedback_txt_length"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="10dp"
android:text="@string/feedback_ed_length" />
</LinearLayout>
<!-- 如果不想使EditText进入界面就获取焦点,可以使用 requestFocus 交给其他控件 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/feedback_user"
android:textSize="@dimen/title_txt_size" >
<requestFocus />
</TextView>
// 【是add不是set注意啦】添加输入监听,显示输入字数。最好是在文字变化之后获取文字的长度,
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 此方法有问题,当删除文字的时候会出现错误
// tvLength.setText((start + count) + "/400");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
//设置长度,以及其他操作,数字变红之类的
tvLength.setText(String.format(getResources().getString(R.string.feedback_ed_length), s.length()));
}
});
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<!-- 上面的布局,包在一个Layout中,当弹出输入法的时候,布局还可以滚动,并且这里面不包含顶部标题,就不会将标题顶出屏幕。要与Activity的android:windowSoftInputMode属性进行配合使用 -->
</ScrollView>