Android开发笔记(一)

1. Volley请求接口中不能有空格,可用%20代替。

2. 必要时可以重写Activity的onRestart()方法,在重新返回界面时进行刷新数据。

3. progressDialog.setCancelable(true),点击其他地方取消弹出框。

4. shape在selector里面使用:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_item_press_corner" android:state_pressed="true"/>
    <item android:drawable="@drawable/shape_item_white_corner"/>
</selector>

其中 @drawable/shape_item_press_corner

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/color_gray_item_pressed" />
    <corners android:radius="5dp" />
</shape>

其中 shape_item_white_corner

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/color_white" />
    <corners android:radius="5dp" />
</shape>

5. Intent传值不能超过40k,否则报错。

6. 判断jsonObject是否为空:

if (resultObj.isNull("gpsTime")) {
    isNull = true;
    Log.e(TAG, "resultObj-isNUll->" + isNull);
} else {
    gpsTime = resultObj.getString("gpsTime");
    longitude = resultObj.getDouble("longitude");
    latitude = resultObj.getDouble("latitude");
    Log.e(TAG, "response_result:: \nlongitude-->" + longitude + "\nlatitude-->" + latitude);
}

7. 横竖屏切换问题,如果不想重复调用activity生命周期,需要在之前的基础上再添加一个属性:

android:configChanges="keyboardHidden|orientation|screenSize"
这是安卓3.2之后的改动!
android:screenOrientation="portrait"  始终竖屏显示
android:screenOrientation="landscape" 始终横屏显示

8. AlertDiaglog实现 点击确定按钮不消失 功能

.setPositiveButton("设置", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        if (dateTimeListener != null) {
            if (date.compareTo(new Date()) > 0) {
                // 条件不成立不能关闭 AlertDialog 窗口
                try {
                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                    field.setAccessible(true);
                     field.set(dialog, false); // false - 使之不能关闭(此为机关所在,其它语句相同)
                    UiToast.showToastShort("所选日期大于当前日期,请重新选择!", activity);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                    e.printStackTrace();
                }
            } else {
                // 条件成立能关闭 AlertDialog 窗口
                Log.e(TAG,"条件成立,关闭窗口");
                try {
                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                    field.setAccessible(true);
                    field.set(dialog, true); // true - 使之可以关闭(此为机关所在,其它语句相同)
                    dateTimeListener.onSet(date);
                    ad.cancel();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        try {
            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}).show();

9. 在广播或者服务里面进行startActivity(intent) 调用时。 需要给该Intent设置标志位

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);.

否则报错。

10. 如何防止按钮被迅速多次点击?

public abstract class NoDoubleClickListener implements View.OnClickListener {
    public static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
            lastClickTime = currentTime;
            onNoDoubleClick(v);
        }
    }
    public abstract void onNoDoubleClick(View v);
}

使用的时候继承

onNoDoubleClick()
.setOnClickListener(new NoDoubleClickListener() {
    @Override
    public void onNoDoubleClick(View v) {
        //事件处理
    }
});

11. 有关输入法覆盖编辑框的问题:
在代码中的setContentVIew之前设置:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

12. 代码中判断某个权限是否被开启?

public static int getPermission(Context mActivity, String perm) {
    PackageManager pm = mActivity.getPackageManager();
    boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission(perm, "com.myaudicloud"));
    if (permission) {
        return 1;
    } else {
        return 0;
    }
}

13. 如何去掉进入某个activity时因延迟造成的留白?
在清单l文件中,找到对应的activity,然后添加属性:android:theme=”@style/activityTheme”
在styles.xml文件中添加:

<!-- Activity主题 -->
<style name="activityTheme" parent="@style/AppTheme">
    <item name="android:windowIsTranslucent">true</item>
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值