我们做项目中有时会遇到这样的情况,适配器布局中假如显示的数据最多有4行,最少只有2行。这样的话就需要我们判断没有数据的时候就把控件隐藏而且不让它站空间。代码如下:
//地址为空
if (info.getEa_address() == null) {
//隐藏控件
holder.EssentialAreas_address.setVisibility(View.GONE);
holder.EssentialAreas_img2.setVisibility(View.GONE);
}else {
//不为空显示内容
holder.EssentialAreas_address.setText(info.getEa_address());
holder.EssentialAreas_img2.setImageResource(R.drawable.village_address_icon);
}
用textView显示时,如果显示的字数太多会感觉不怎么美观,就要对textView显示做处理规定显示多少个字数,多的就会用省略号显示。这个一般在布局文件中设置。
<TextView
android:id="@+id/newspaper_report_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="6"
android:singleLine="true"
android:ellipsize="end"
android:text="dsadsad"
android:textColor="@color/color_gray_dark"
android:textSize="@dimen/tv_size_large" />
在editText输入框中果然后台存储的字符有限,这样我们就要对editText输入的字符做限制,我们可以在布局文件中做处理,也可以用代码来做处理。布局文件如下:
<EditText
android:id="@+id/essential_areas_add_et_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:textColor="@color/color_gray_dark"
android:textSize="@dimen/tv_size_normal"
android:maxLength="10" >
用代码控制editText输入字符
public class ReplyActivity extends MyBaseActivity implements {
private TextView counts;
private EditText content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.replyactivity);
//初始化布局
InitView();
//事件监听
InitEvent();
}
private void InitView() {
content = (EditText) findViewById(R.id.reply_content);
counts = (TextView) findViewById(R.id.reply_text_counts);
}
private void InitEvent() {
//监听editText中的字符
content.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int selectionStart;
private int selectionEnd;
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
temp = s;
}
//editText中字符的变化
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
//变化的字符显示在textView中
counts.setText(s.length() + "/200");
}
@Override
public void afterTextChanged(Editable s) {
selectionStart = content.getSelectionStart();
selectionEnd = content.getSelectionEnd();
//设置editText中最多能输入多少字符
if (temp.length() > 200) {
Toast.makeText(ReplyActivity.this, "输入的内容超过限制!",
Toast.LENGTH_SHORT).show();
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionStart;
content.setText(s);
content.setSelection(tempSelection);
}
}
});
}
}
图如下:
在editText中有时候只能让用户输入数字,如手机号码。我们只要在布局文件中设置
<EditText
android:id="@+id/essential_areas_add_et_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ems="10"
android:inputType="number"
android:textColor="@color/color_gray_dark"
android:textSize="@dimen/tv_size_normal" >
</EditText>
用户要输入的内容比较多,给一行输入显得太小气,我们就要把editText的高变大点,我们就这样设置:
<EditText
android:id="@+id/essential_areas_add_et_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="left|top"
android:inputType="textMultiLine"
android:minLines="6"
android:textColor="@color/color_gray_dark"
android:textSize="@dimen/tv_size_normal" >
</EditText>
在适配器的布局中如果适配器的布局过于复杂,里面还包括GridView或listView在主活动中显示就会失去点击的焦点,这样一来我们在listView中就没办法点击其中的每一个子列,我们就要在布局文件中设置一个属性:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
</LinearLayout>

被折叠的 条评论
为什么被折叠?



