Android开发技巧总结

本文总结了Android开发中的一些技巧和常见问题,包括获取全局Context、使用autoLink属性、动态设置文字大小、EditText禁止换行、RecyclerView相关问题、TextView加载html、沉浸式模式、获取当前启动Activity、启动黑屏问题等,并提供了相应的解决方法。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值