版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chaoyangsun/article/details/80177454
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组件加载!
//适合加载纯文本
textView.setText(Html.fromHtml(html));
//可以加载带图片的html 如果是网络图片,还需要异步加载
textView.setText(Html.fromHtml(string, imgGetter, null));
第一种加载纯文本的情况不多说,下面重点说一说异步加载图片的情况:
public void parseHtml(final String html) {
final Html.ImageGetter imgGetter = new Html.ImageGetter() //格式语句不一定相同,只要进行网络加载图片即可
{
public Drawable getDrawable(String source)
{
Drawable drawable = null;
try
{
drawable = Drawable.createFromStream(new URL(source).openStream(), "");//加载网络图片资源核心语句
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getIntrinsicHeight());
}
catch (Exception e)
{
return new Drawable()
{
public void setColorFilter(ColorFilter cf) {}
public void setAlpha(int alpha) {}
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
public void draw(Canvas canvas) {}
};
}
return drawable;
}
};
new Thread(new Runnable()
{
@Override
public void run()
{
final Spanned text = Html.fromHtml(html,imgGetter,null);
handler.post(new Runnable()
{
@Override
public void run()
{
introServer.setText(text);
}
});
}
}).start();
}
}