1、获取全局Context
编写Application
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
//获取Context
context = getApplicationContext();
}
//返回
public static Context getContextObject(){
return context;
}
}
调用
MyApplication.getContextObject();
注:application需要注册
<application
android:name="包名.MyApplication"
....
>
2、使用autoLink属性 跳转网页,拨打电话,发送邮件等
只需android:autoLink=“web” 点击就能自动跳转网页,简单粗暴
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="www.jeean.cn "/>
只需android:autoLink=“phone” 点击就能自动拨打电话,一样的简单粗暴
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="18888888888"/>
3、代码中动态设置文字大小时,单位的问题
借助TypedValue设置单位
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//14sp
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);//15dp
4、禁止EditText换行
singleLine已经过时,需要使用下面两个属性才能起到作用:
android:lines="1"
android:inputType="text"
5、recyclerview使用notifyDataSetChanged时,item里面EditText显示错乱或清空
解决方案有两种,这里只说一种最简单的,应为一般item里面使用EditText时,列表长度都是很有限的,所以直接禁止recyclerview的item的复用就行:
@Override
public void bindViewHolder(final FlexibleAdapter adapter, EntityViewHolder holder, final int position, List payloads) {
holder.setIsRecyclable(false);
...
holder.editText.setText(value);
holder.et_content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
value = editable.toString();
}
});
}
6、recyclerview隐藏某个item后,该item仍然占位的问题
如果只是简单的设置隐藏,那么item虽然不可见了,但是其宽高依然占位,解决办法:
@Override
public void bindViewHolder(final FlexibleAdapter adapter, EntityViewHolder holder, final int position, List payloads) {
RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
itemView.setVisibility(View.GONE);
param.height = 0;
param.width = 0;
itemView.setLayoutParams(param);
}
7、TextView加载html并异步加载html中的网络图片
加载html一般使用webview,有一些情况,比如html比较简单或xml布局文件不适合嵌入webview,此时可以用TextView组件加载!
//适合加载纯文本
textV