基础知识简单小记

1.设置进度条

ProgressDialog progressDlg = ProgressDialog.show(ModifyPasswordActivity.this, null, "请稍候...", true);
progressDlg.setCancelable(false); //点击不可取消
if (progressDlg != null)
   progressDlg.dismiss();//不用时,取消

2.activity的启动模式

Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance。

常用flag说明
FLAG_ACTIVITY_CLEAR_TOP:清除站内位于该activity之上的所有activity。即若目标Activity在栈中已经存在,则将会把位于该目标activity之上的activity从栈中弹出销毁。

FLAG_ACTIVITY_NEW_TASK:如果试图从非activity的非正常途径启动一个activity,比如从一个service中启动一个activity,则intent比如要添加FLAG_ACTIVITY_NEW_TASK 标记。

FLAG_ACTIVITY_NO_HISTORY:启动的activity不会被压入栈中。

FLAG_ACTIVITY_SINGLE_TOP:如果某个intent添加了这个标志,并且这个intent的目标activity就是栈顶的activity,那么将不会新建一个实例压入栈中。

关于该问题,郭大侠的文章可以更好的参考,网站:
Android任务和返回栈完全解析,细数那些你所不知道的细节
http://blog.csdn.net/guolin_blog/article/details/41087993

3.窗口透明的两种方式

1)在style中直接继承透明主题

android:theme="@android:style/Theme.Translucent.NoTitleBar"

2)重写style中的windowIsTranslucent和windowBackground属性

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>    
</style>

4.intent传递数据

发送方

Intent intent = new Intent(A.this, B.class);
intent.putExtra("str","ddddd");
startActivity(intent);

接收方

Intent intent = this.getIntent();
if(intent.hasExtra("str")){
     account.setText(intent.getExtras().getString(str));
}

5.android中的字符串的简单加减

一般以BigDecimal类型进行转换,可将别的类型先转为BigDecimal类型,然后再进行计算:

5.1 构造器描述

BigDecimal(int)       创建一个具有参数所指定整数值的对象。 
BigDecimal(double) 创建一个具有参数所指定双精度值的对象。 
BigDecimal(long)    创建一个具有参数所指定长整数值的对象。 
BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。

5.2 方法描述

add(BigDecimal)        BigDecimal对象中的值相加,然后返回这个对象。 
subtract(BigDecimal)   BigDecimal对象中的值相减,然后返回这个对象。 
multiply(BigDecimal)   BigDecimal对象中的值相乘,然后返回这个对象。 
divide(BigDecimal)     BigDecimal对象中的值相除,然后返回这个对象。 
toString()             将BigDecimal对象的数值转换成字符串。 
doubleValue()          将BigDecimal对象中的值以双精度数返回。 
floatValue()           将BigDecimal对象中的值以单精度数返回。 
longValue()            将BigDecimal对象中的值以长整数返回。 
intValue()             将BigDecimal对象中的值以整数返回。

6.Context的属性 Android Context 上下文 你必须知道的一切

http://blog.csdn.net/lmj623565791/article/details/40481055

凡是跟UI相关的,都应该使用Activity做为Context来处理;
其他的一些操作,Service,Activity,Application等实例都可以,当然了,注意Context引用的持有,防止内存泄漏。

7.判断字符串是否为手机号码

public static boolean isMobileNum(String mobiles) {
    Pattern p = Pattern
            .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
    Matcher m = p.matcher(mobiles);
    System.out.println(m.matches() + "---");
    return m.matches();
}

上述方法有些号码会被过滤掉,以下方法更为通用

/*判断字符串是否为手机号码*/
public static boolean isMobileNum(String mobiles) {
    String newString = mobiles.replace("-", "").toString().replace(" ", "");
    Pattern p = Pattern.compile("^((1[0-9]))\\d{9}$");
    Matcher m = p.matcher(newString.trim());
    return m.matches();
}

/*判断字符串中是否存在中文*/
public static boolean isHaveHanZi(String hanzi){
    boolean ishanzi = (hanzi.getBytes().length != hanzi.length());
    return ishanzi;
}

8.match_parent width does not work in RecyclerView

在加载布局的时候,重新设置match_parent属性

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);///重新设置match_parent属性
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);

9.系统资源回收

@Override  
protected void onDestroy() { 
	// TODO Auto-generated method stub  
	super.onDestroy();  
	if(bitmap!=null){  
		if(!bitmap.isRecycled()){  
			bitmap.recycle();   //回收图片所占的内存  
			bitmap=null;  
			System.gc();  //提醒系统及时回收  
		}  
	}  
}  

10.Comparator

1)简单排序,可用list的排序方法,在compare重写比较条件,如:首字母排序

Collections.sort(list, new Comparator<Map<String, Object>>() {
		@Override
		public int compare(Map<String, Object> o1, Map<String, Object> o2) {
			String map1value = ((String) o1.get("firstLatter")).toUpperCase();
			String map2value = ((String) o2.get("firstLatter")).toUpperCase();
			return map1value.compareTo(map2value);
		}
	});

2)多条件排序,可以使用上述的方法,也可以在javaBean中implements Comparable,如下:

/**
 * public static int compare(long lhs, long rhs) {
 * return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
 * }
 * 多重条件的比较方法,首先比较topNum ,其次 choiceNum,最后比较name或goodsName的拼音
 */
@Override
public int compareTo(@NonNull GoodsItemBean another) {
	if (this.topNum.compareTo(another.topNum) != 0) {
		return -(this.topNum.compareTo(another.topNum));
	} else if (this.choiceNum.compareTo(another.choiceNum) != 0) {
		return -(this.choiceNum.compareTo(another.choiceNum));
	} else if (!TextUtils.isEmpty(this.name) && !TextUtils.isEmpty(another.name)) {
		return StringUtil.getPinyin(this.name).compareTo(StringUtil.getPinyin(another.name));
	} else {
		return 0;
	}
}

11.广播接收器

广播接收器的onreceive方法中,不能添加过多的逻辑和做太多耗时操作,不允许开启线程,
	当onReceive()方法运行了较长时间而没有结束时,程序会报错。《第一行代码》P177
	自己发送广播:
	Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
	sendBroadcast(intent);

12. 实时刷新时间,通过系统广播实现

系统每分钟都会发送广播Intent.ACTION_TIME_TICK,可用于分钟更新:

	//onCreateView中
	IntentFilter filter=new IntentFilter();//创建意图过滤器对象
	filter.addAction(Intent.ACTION_TIME_TICK);//为接收器指定action,使之用于接收同action的广播
	view.getContext().registerReceiver(receiver,filter);//动态注册广播接收器
	//view.getContext 是一个 fragment 中的 context   

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_TIME_TICK)) {
                //TODO更新时间
            }
        }
    }; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值