Android 笔记

来源:互联网 发布:win10 1701端口打不开 编辑:程序博客网 时间:2024/06/14 05:52

一:
Android5.0以上系统,在全屏模式下, android:windowSoftInputMode=”adjustResize” 属性不起作用, 键盘弹出时界面没有重新布局。

解决办法:调用AndroidBug5497Workaround.assistActivity(activity)。

AndroidBug5497Workaround代码:

public class AndroidBug5497Workaround {    // For more information, see https://code.google.com/p/android/issues/detail?id=5497    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.    static AndroidBug5497Workaround s_AndroidBug5497Workaround = null;    public static void assistActivity (Activity activity) {        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)        {            //Single AndroidBug5497Workaround            if (s_AndroidBug5497Workaround == null)                s_AndroidBug5497Workaround = new AndroidBug5497Workaround(activity);        }    }    private View mChildOfContent;    private int usableHeightPrevious;    private FrameLayout.LayoutParams frameLayoutParams;    private AndroidBug5497Workaround(Activity activity) {        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);        mChildOfContent = content.getChildAt(0);        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            public void onGlobalLayout() {                possiblyResizeChildOfContent();            }        });        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();    }    private void possiblyResizeChildOfContent() {        int usableHeightNow = computeUsableHeight();        if (usableHeightNow != usableHeightPrevious) {            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();            int heightDifference = usableHeightSansKeyboard - usableHeightNow;            if (heightDifference > (usableHeightSansKeyboard/4)) {                // keyboard probably just became visible                   frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;            } else {                // keyboard probably just became hidden                frameLayoutParams.height = usableHeightSansKeyboard;            }            mChildOfContent.requestLayout();            usableHeightPrevious = usableHeightNow;        }    }    private int computeUsableHeight() {        Rect r = new Rect();        mChildOfContent.getWindowVisibleDisplayFrame(r);        return (r.bottom - r.top);    }}

二:
Thread.getStackTrace() Returns an array of {@link StackTraceElement} representing the current thread’s stack.

三:
检查是否有鼠标插入

private void checkMouse(){    mHasMouse = false;    final int[] devices = mInputManager.getInputDeviceIds();    for (int id : devices) {        InputDevice device = mInputManager.getInputDevice(id);          if (device != null)         {            Log.d("FrmMainWork", device.toString());            boolean isExternal = false;            if (mMethodisExternal != null)            {                try {                    isExternal = (boolean) mMethodisExternal.invoke(device);                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if (!device.isVirtual() && isExternal &&                     (device.getName().contains("Mouse") || device.getName().contains("Touch")))            {                mHasMouse = true;            }        }    }    Log.d("FrmMainWork", "checkMouse:" + mHasMouse);}

在InputDeviceListener中调用
mInputManager = (InputManager) activity.getSystemService(Context.INPUT_SERVICE);
mInputManager.registerInputDeviceListener(mInputDeviceListener, null);

以上代码中的
Method mMethodisExternal = InputDevice.class.getDeclaredMethod(“isExternal”);
在InputDevice中,

/** * Returns true if the device is external (connected to USB or Bluetooth or some other * peripheral bus), otherwise it is built-in. * * @return True if the device is external. * * @hide */public boolean isExternal() {    return mIsExternal;}

isExternal()函数被隐藏调了,故需要通过反射获取。

四:
全屏显示的方式

    if (hasFocus) {        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)        {            getWindow().getDecorView().setSystemUiVisibility(                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                    | View.SYSTEM_UI_FLAG_FULLSCREEN                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);        }    }

View.setSystemUiVisibility(int visibility);函数:

public void setSystemUiVisibility(int visibility) {    if (visibility != mSystemUiVisibility) {        mSystemUiVisibility = visibility;        if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {            mParent.recomputeViewAttributes(this);        }    }}

在没有焦点时不要去设置,没有焦点时设置并不会有效果, 而mSystemUiVisibility = visibility赋值会完成,下次有焦点时就可能会因为visibility == mSystemUiVisibility而直接返回,达不到想要的效果。(不太确定,自己的推测)

五:
照片解析时,若使用Bitmap bitmap = BitmapFactory.decodeFile(picDir);来获取Bitmap, 此时获取的bitmap可能与相册显示的不同, 可能会有旋转。
解决办法:

    ExifInterface exif = new ExifInterface(picDir);        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);            Bitmap bitmap = BitmapFactory.decodeFile(picDir);            Matrix matrix = new Matrix();            if(bitmap != null){                switch (orientation) {                case 3:                    matrix.postRotate(180);                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);                    break;                case 6:                    matrix.postRotate(90);                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);                    break;                case 8:                    matrix.postRotate(-90);                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);                    break;                default:                    break;                }            }

六:

        getWindow().getDecorView().setSystemUiVisibility(                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_FULLSCREEN);

会使android:windowSoftInputMode=”adjustResize”达不到想要的效果

七:
全屏模式下,使弹出对话框时,statusbar和navigationbar不出现

            // Set the dialog to not focusable (makes navigation ignore us adding the window)            customDialog.getWindow().setFlags(                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);            // Show the dialog!            customDialog.show();            // Set the dialog to immersive            customDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);

八:
用Gson库解析Json数据时,解析派生类时会出错。
解决示例:

public class QuestionSerializer implements JsonSerializer<Question>,    JsonDeserializer<Question> {    // 对象转为Json时调用,实现JsonSerializer<PackageState>接口    @Override    public JsonElement serialize(Question question, Type arg1,            JsonSerializationContext arg2) {        if (question instanceof ChoiceQuestion) {            return arg2.serialize((ChoiceQuestion) question);        } else if (question instanceof TrueFalseQuestion) {            return arg2.serialize((TrueFalseQuestion) question);        }        Gson gson = new Gson();        return gson.toJsonTree(question);    }    // json转为对象时调用,实现JsonDeserializer<PackageState>接口    @Override    public Question deserialize(JsonElement json, Type typeOfT,                JsonDeserializationContext arg2) throws JsonParseException {        JsonObject jobject = json.getAsJsonObject();        if (jobject.has("type")) {            if (jobject.getAsJsonPrimitive("type").getAsInt() == DataStruct.CHOICE_QUESTION) {                ChoiceQuestion question = arg2.deserialize(json, ChoiceQuestion.class);                return question;            } else if (jobject.getAsJsonPrimitive("type").getAsInt() == DataStruct.TRUEFALSE_QUESTION) {                TrueFalseQuestion question = arg2.deserialize(json, TrueFalseQuestion.class);                return question;            }        }        Gson gson = new Gson();        Question question = gson.fromJson(json, Question.class);        return question;    }}GsonBuilder gsonBuilder;gsonBuilder = new GsonBuilder();gsonBuilder.registerTypeAdapter(Question.class,new QuestionSerializer());gson = gsonBuilder.create();

用这样生成的gson去解析即可

九:

标签用于指定屏幕内的焦点View。

例如我们点击tab键或enter键焦点自动进入下一个输入框
用法: 将标签置于Views标签内部

        <EditText id="@+id/text"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:layout_weight="0"                     android:paddingBottom="4">               <requestFocus />        </EditText>
0 0
原创粉丝点击