Android[控件]NumberPicker使用问题汇总

NumberPicker的核心函数:
1、setDisplayedValues

    /**
     * Sets the values to be displayed.
     *
     * @param displayedValues The displayed values.
     *
     * <strong>Note:</strong> The length of the displayed values array
     * must be equal to the range of selectable numbers which is equal to
     * {@link #getMaxValue()} - {@link #getMinValue()} + 1.
     */
    public void setDisplayedValues(String[] displayedValues) {
        if (mDisplayedValues == displayedValues) {
            return;
        }
        mDisplayedValues = displayedValues;
        if (mDisplayedValues != null) {
            // Allow text entry rather than strictly numeric entry.
            mInputText.setRawInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        } else {
            mInputText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
        }
        updateInputTextView();
        initializeSelectorWheelIndices();
        tryComputeMaxWidth();
    }

2、setMaxValue

    /**
     * Sets the max value of the picker.
     *
     * @param maxValue The max value inclusive.
     *
     * <strong>Note:</strong> The length of the displayed values array
     * set via {@link #setDisplayedValues(String[])} must be equal to the
     * range of selectable numbers which is equal to
     * {@link #getMaxValue()} - {@link #getMinValue()} + 1.
     */
    public void setMaxValue(int maxValue) {
        if (mMaxValue == maxValue) {
            return;
        }
        if (maxValue < 0) {
            throw new IllegalArgumentException("maxValue must be >= 0");
        }
        mMaxValue = maxValue;
        if (mMaxValue < mValue) {
            mValue = mMaxValue;
        }
        updateWrapSelectorWheel();
        initializeSelectorWheelIndices();
        updateInputTextView();
        tryComputeMaxWidth();
        invalidate();
    }

3、setMinValue

    /**
     * Sets the min value of the picker.
     *
     * @param minValue The min value inclusive.
     *
     * <strong>Note:</strong> The length of the displayed values array
     * set via {@link #setDisplayedValues(String[])} must be equal to the
     * range of selectable numbers which is equal to
     * {@link #getMaxValue()} - {@link #getMinValue()} + 1.
     */
    public void setMinValue(int minValue) {
        if (mMinValue == minValue) {
            return;
        }
        if (minValue < 0) {
            throw new IllegalArgumentException("minValue must be >= 0");
        }
        mMinValue = minValue;
        if (mMinValue > mValue) {
            mValue = mMinValue;
        }
        updateWrapSelectorWheel();
        initializeSelectorWheelIndices();
        updateInputTextView();
        tryComputeMaxWidth();
        invalidate();
    }

4、setValue

    /**
     * Set the current value for the number picker.
     * <p>
     * If the argument is less than the {@link NumberPicker#getMinValue()} and
     * {@link NumberPicker#getWrapSelectorWheel()} is <code>false</code> the
     * current value is set to the {@link NumberPicker#getMinValue()} value.
     * </p>
     * <p>
     * If the argument is less than the {@link NumberPicker#getMinValue()} and
     * {@link NumberPicker#getWrapSelectorWheel()} is <code>true</code> the
     * current value is set to the {@link NumberPicker#getMaxValue()} value.
     * </p>
     * <p>
     * If the argument is less than the {@link NumberPicker#getMaxValue()} and
     * {@link NumberPicker#getWrapSelectorWheel()} is <code>false</code> the
     * current value is set to the {@link NumberPicker#getMaxValue()} value.
     * </p>
     * <p>
     * If the argument is less than the {@link NumberPicker#getMaxValue()} and
     * {@link NumberPicker#getWrapSelectorWheel()} is <code>true</code> the
     * current value is set to the {@link NumberPicker#getMinValue()} value.
     * </p>
     *
     * @param value The current value.
     * @see #setWrapSelectorWheel(boolean)
     * @see #setMinValue(int)
     * @see #setMaxValue(int)
     */
    public void setValue(int value) {
        setValueInternal(value, false);
    }
 
    /**
     * Sets the current value of this NumberPicker.
     *
     * @param current The new value of the NumberPicker.
     * @param notifyChange Whether to notify if the current value changed.
     */
    private void setValueInternal(int current, boolean notifyChange) {
        if (mValue == current) {
            return;
        }
        // Wrap around the values if we go past the start or end
        if (mWrapSelectorWheel) {
            current = getWrappedSelectorIndex(current);
        } else {
            current = Math.max(current, mMinValue);
            current = Math.min(current, mMaxValue);
        }
        int previous = mValue;
        mValue = current;
        updateInputTextView();
        if (notifyChange) {
            notifyChange(previous, current);
        }
        initializeSelectorWheelIndices();
        invalidate();
    }

5、setFormatter

    /**
     * Set the formatter to be used for formatting the current value.
     * <p>
     * Note: If you have provided alternative values for the values this
     * formatter is never invoked.
     * </p>
     *
     * @param formatter The formatter object. If formatter is <code>null</code>,
     *            {@link String#valueOf(int)} will be used.
     *@see #setDisplayedValues(String[])
     */
    public void setFormatter(Formatter formatter) {
        if (formatter == mFormatter) {
            return;
        }
        mFormatter = formatter;
        initializeSelectorWheelIndices();
        updateInputTextView();
    }

仔细观察实现函数,发现它们都会触发一个内部函数updateInputTextView




    /**
     * Updates the view of this NumberPicker. If displayValues were specified in
     * the string corresponding to the index specified by the current value will
     * be returned. Otherwise, the formatter specified in {@link #setFormatter}
     * will be used to format the number.
     *
     * @return Whether the text was updated.
     */
    private boolean updateInputTextView() {
        /*
         * If we don't have displayed values then use the current number else
         * find the correct value in the displayed values for the current
         * number.
         */
        String text = (mDisplayedValues == null) ? formatNumber(mValue)
                : mDisplayedValues[mValue - mMinValue];
        if (!TextUtils.isEmpty(text) && !text.equals(mInputText.getText().toString())) {
            mInputText.setText(text);
            return true;
        }
 
        return false;
    }

这里如果mDisplayedValues不为空时,显示索引为mValue – mMinValue的值!!!
看到这里,大家心里应该有警惕了,如果mDisplayedValues的长度变短了……而索引还未改变……索引就会越界……
那么明显的,改变显示的数据集,长度发生变化的,上面的一系列函数调用顺序就需要好好思量了
a)当displayedValues长度变大时
i、setDisplayedValues
ii、setMaxValue
iii、setMinValue
iV、setValue
b)否则
i、setMaxValue
ii、setMinValue
iii、setDisplayedValues
iV、setValue
a情况,先扩张displayedValues容量,索引仍然保持不变
b情况,先缩小maxValue的值,同时会限制value也缩小,同步的索引也变小,最后赋值displayedValues

文章本应到此为止,可是实际上,在setValue之后,NumberPicker中间的文本并没有显示对应的值,原本以为
添上invalidate就可以了,结果是不行;在界面点击中间的文本后,文字会刷新显示正确。
解决办法如下代码:




    /**
     * NumberPicker EditText
     */
    public static void applyNumberPickerEditTextStyle(NumberPicker picker, String text) {
        EditText editText = null;
        for (int i = 0; i < picker.getChildCount(); i++) {
            View child = picker.getChildAt(i);
            if (child instanceof EditText) {
                editText = (EditText) child;
                break;
            }
        }
        if (editText != null) {
            // disable text input
            editText.setFocusable(false);
            editText.setFilters(new InputFilter[0]);
            editText.setText(text);
        }
    }

AndroidStudio编译异常com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

今天遇到了传说中的“方法数超65536”的问题,贴出Gradle异常如下:

 
FAILURE: Build failed with an exception.
 
* What went wrong:
Execution failed for task ':ToddlerCare:transformClassesWithDexForInnertestJpushNochannelDebug'.
> com.android.build.api.transform.TransformException: 
  com.android.ide.common.process.ProcessException: 
  java.util.concurrent.ExecutionException: 
  com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
 
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
 
BUILD FAILED
 
Total time: 5 mins 28.887 secs

先贴出官方解决方案的链接:
http://developer.android.com/tools/building/multidex.html

再贴出stackoverflow问题链接:
http://stackoverflow.com/questions/27377080/after-update-of-as-to-1-0-getting-method-id-not-in-0-0xffff-65536-error-i

摘录解决步骤如下:
1、在build.gradle中,设置multiDexEnabled:

android {
   defaultConfig {
      multiDexEnabled true
   }
}

2、引入multidex库

dependencies {
   compile 'com.android.support:multidex:1.0.0'
}

3、将你的Aplication继承自MultiDexApplication,或是在manifest中指定application的属性

android:name="android.support.multidex.MultiDexApplication"

如果你的Application继承自别的类,那就需要重写attachBaseContext

protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

Android下载文件异常java.net.ProtocolException: unexpected end of stream

在使用HttpURLConnection下载文件的时候,如果服务器返回的Content-Length字段给出的文件长度比实际文件大小大的时候,客户端在读取流数据到达实际文件大小后,继续读取数据时,就会抛出
java.net.ProtocolException: unexpected end of stream
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:421)
at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:349)
at java.io.BufferedInputStream.read(BufferedInputStream.java:290)
at java.io.BufferedInputStream.read(BufferedInputStream.java:290)
……

WordPress设置字体

WordPress主题默认使用的字体“font-family: Merriweather, Georgia, serif;”在显示英文字符的时候,会忽上忽下、忽大忽小,而且阿拉伯数字惨目忍睹,所以急需替换该字体。
自行查找Style.css文件,找到“3.0 – Typography”,在“font-family: “后插入想要设置的字体,例如本站的Menlo,是Mac OS X下Android Studio默认的编辑字体。

附录:

MacOS Windows 10 Ubuntu
Sublime Text Menlo Consolas Monospace
Atom Menlo Consolas DejaVu Sans Mono
IntelliJ Idea Menlo Monospace DejaVu Sans Mono
Eclipse Monaco Consolas Monospace
Visual Studio Consolas
Visual Studio Code Menlo Consolas Droid Sans Mono
XCode Menlo
Notepad++ Courier New
Spacemacs Source Code Pro Source Code Pro Source Code Pro

表格来源:http://blog.jobbole.com/104090/?utm_source=hao.jobbole.com&utm_medium=relatedArticle

参考资源:https://www.anotherhome.net/1010

WordPress文章中插入代码

在上篇文章中讲到了如何设置ICP备案链接的问题,百度搜索到的好多文章都能将代码显示在一块表格区域,于是好奇心大起;使用Chrome浏览器的开发者工具点开详细,发现div标签使用的是wp-syntax,于是搜索到WP-Syntax插件,非常好用,上篇例子中用到的是其xml语言。
使用方法如下:
1、在html文本编辑中插入代码<pre lang=”xml” escaped=”true”>
2、插入你要展示的代码
3、插入结尾符</pre>

WordPress添加ICP备案链接

  • 在仪表盘界面打开“设置”->“常规”,填入“ICP备案号”项
  • 仪表盘点开“外观”->“编辑”,右侧“模版”选中“主题页脚(footer.php)”
  • 编辑页脚页面内容,插入超链接:
    <a href="http://www.miitbeian.gov.cn/" target=""_blank”" rel="external nofollow">
    <?php echo get_option( 'zh_cn_l10n_icp_num' );?>
    </a>