Android 零碎知识点---2
Posted yarsnwoing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 零碎知识点---2相关的知识,希望对你有一定的参考价值。
android中TextView属性之autoText解析
(转载自:http://blog.csdn.net/muyi_amen/article/details/46911635)
如果没有时间看完全篇的同学们,可以直接看文章的最后第四点总结的内容。
一、看看Android官方文档的描述。
在这之前,需要说明一点autoText虽然是TextView的属性之一,但它实际是服务于EditText。EditText是TextView的子类。
如果在TextView中设置属性autoText,编译器会提示“TextView不建议使用autoText属性”—因为设置了也是无效的。
二、那autoText在EditText中到底在干什么呢。来看看android TextView源码片段。
[java] view plain copyprint?
1. //autotext对应的是android:autotext属性
2. //autocap对应的是android:capitalize属性
3. if (autotext || autocap != -1)
4. TextKeyListener.Capitalize cap;
5.
6. inputType = EditorInfo.TYPE_CLASS_TEXT;
7.
8. switch (autocap)
9. case 1:
10. cap = TextKeyListener.Capitalize.SENTENCES;//每句的第一个单词首字母转为大写
11. //inputType对应的是android:inputType属性
12. inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES;//每一句的第一个单词的首字母大写
13. break;
14.
15. case 2:
16. cap = TextKeyListener.Capitalize.WORDS;//每个单词的首字母大写
17. inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS;
18. break;
19.
20. case 3:
21. cap = TextKeyListener.Capitalize.CHARACTERS;//所有字符大写
22. inputType |= EditorInfo.TYPE_TEXT_FLAG_CAP_CHARACTERS;
23. break;
24.
25. default:
26. cap = TextKeyListener.Capitalize.NONE;
27. break;
28.
29.
30. createEditorIfNeeded();
31. mEditor.mKeyListener = TextKeyListener.getInstance(autotext, cap);
32. mEditor.mInputType = inputType;
33.
此段源码中可以发现,其实就是给inputType这个属性赋值。
第31行中,再次把autoText传递给了TextKeyListener。看看TextKeyListener的源码:
[java] view plain copyprint?
1. public class TextKeyListener extends BaseKeyListener implements SpanWatcher
2. private static TextKeyListener[] sInstance =
3. new TextKeyListener[Capitalize.values().length * 2];
4.
5. /* package */ static final Object ACTIVE = new NoCopySpan.Concrete();
6. /* package */ static final Object CAPPED = new NoCopySpan.Concrete();
7. /* package */ static final Object INHIBIT_REPLACEMENT = new NoCopySpan.Concrete();
8. /* package */ static final Object LAST_TYPED = new NoCopySpan.Concrete();
9.
10. private Capitalize mAutoCap;
11. private boolean mAutoText;
12.
13. private int mPrefs;
14. private boolean mPrefsInited;
15.
16. /* package */ static final int AUTO_CAP = 1;
17. /* package */ static final int AUTO_TEXT = 2;
18. /* package */ static final int AUTO_PERIOD = 4;
19. /* package */ static final int SHOW_PASSWORD = 8;
20. private WeakReference<ContentResolver> mResolver;
21. private TextKeyListener.SettingsObserver mObserver;
22.
23. /**
24. * Creates a new TextKeyListener with the specified capitalization
25. * and correction properties.
26. *
27. * @param cap when, if ever, to automatically capitalize.
28. * @param autotext whether to automatically do spelling corrections.
29. */
30. public TextKeyListener(Capitalize cap, boolean autotext)
31. mAutoCap = cap;
32. mAutoText = autotext;
33.
34.
35. /**
36. * Returns a new or existing instance with the specified capitalization
37. * and correction properties.
38. *
39. * @param cap when, if ever, to automatically capitalize.
40. * @param autotext whether to automatically do spelling corrections.
41. */
42. public static TextKeyListener getInstance(boolean autotext,
43. Capitalize cap)
44. int off = cap.ordinal() * 2 + (autotext ? 1 : 0);
45.
46. if (sInstance[off] == null)
47. sInstance[off] = new TextKeyListener(cap, autotext);
48.
49.
50. return sInstance[off];
51.
52.
53. /**
54. * Returns a new or existing instance with no automatic capitalization
55. * or correction.
56. */
57. public static TextKeyListener getInstance()
58. return getInstance(false, Capitalize.NONE);
59.
60.
61. /**
62. * Returns whether it makes sense to automatically capitalize at the
63. * specified position in the specified text, with the specified rules.
64. *
65. * @param cap the capitalization rules to consider.
66. * @param cs the text in which an insertion is being made.
67. * @param off the offset into that text where the insertion is being made.
68. *
69. * @return whether the character being inserted should be capitalized.
70. */
71. public static boolean shouldCap(Capitalize cap, CharSequence cs, int off)
72. int i;
73. char c;
74.
75. if (cap == Capitalize.NONE)
76. return false;
77.
78. if (cap == Capitalize.CHARACTERS)
79. return true;
80.
81.
82. return TextUtils.getCapsMode(cs, off, cap == Capitalize.WORDS
83. ? TextUtils.CAP_MODE_WORDS : TextUtils.CAP_MODE_SENTENCES)
84. != 0;
85.
86.
87. public int getInputType()
88. return makeTextContentType(mAutoCap, mAutoText);
89.
90.
91. @Override
92. public boolean onKeyDown(View view, Editable content,
93. int keyCode, KeyEvent event)
94. KeyListener im = getKeyListener(event);
95.
96. return im.onKeyDown(view, content, keyCode, event);
97.
98.
99. @Override
100. public boolean onKeyUp(View view, Editable content,
101. int keyCode, KeyEvent event)
102. KeyListener im = getKeyListener(event);
103.
104. return im.onKeyUp(view, content, keyCode, event);
105.
106.
107. @Override
108. public boolean onKeyOther(View view, Editable content, KeyEvent event)
109. KeyListener im = getKeyListener(event);
110.
111. return im.onKeyOther(view, content, event);
112.
113.
114. /**
115. * Clear all the input state (autotext, autocap, multitap, undo)
116. * from the specified Editable, going beyond Editable.clear(), which
117. * just clears the text but not the input state.
118. *
119. * @param e the buffer whose text and state are to be cleared.
120. */
121. public static void clear(Editable e)
122. e.clear();
123. e.removeSpan(ACTIVE);
124. e.removeSpan(CAPPED);
125. e.removeSpan(INHIBIT_REPLACEMENT);
126. e.removeSpan(LAST_TYPED);
127.
128. QwertyKeyListener.Replaced[] repl = e.getSpans(0, e.length(),
129. QwertyKeyListener.Replaced.class);
130. final int count = repl.length;
131. for (int i = 0; i < count; i++)
132. e.removeSpan(repl[i]);
133.
134.
135.
136. public void onSpanAdded(Spannable s, Object what, int start, int end)
137. public void onSpanRemoved(Spannable s, Object what, int start, int end)
138.
139. public void onSpanChanged(Spannable s, Object what, int start, int end,
140. int st, int en)
141. if (what == Selection.SELECTION_END)
142. s.removeSpan(ACTIVE);
143.
144.
145.
146. private KeyListener getKeyListener(KeyEvent event)
147. KeyCharacterMap kmap = event.getKeyCharacterMap();
148. int kind = kmap.getKeyboardType();
149.
150. if (kind == KeyCharacterMap.ALPHA)
151. return QwertyKeyListener.getInstance(mAutoText, mAutoCap);
152. else if (kind == KeyCharacterMap.NUMERIC)
153. return MultiTapKeyListener.getInstance(mAutoText, mAutoCap);
154. else if (kind == KeyCharacterMap.FULL
155. || kind == KeyCharacterMap.SPECIAL_FUNCTION)
156. // We consider special function keyboards full keyboards as a workaround for
157. // devices that do not have built-in keyboards. Applications may try to inject
158. // key events using the built-in keyboard device id which may be configured as
159. // a special function keyboard using a default key map. Ideally, as of Honeycomb,
160. // these applications should be modified to use KeyCharacterMap.VIRTUAL_KEYBOARD.
161. return QwertyKeyListener.getInstanceForFullKeyboard();
162.
163.
164. return NullKeyListener.getInstance();
165.
166.
167. public enum Capitalize
168. NONE, SENTENCES, WORDS, CHARACTERS,
169.
170.
171. private static class NullKeyListener implements KeyListener
172.
173. public int getInputType()
174. return InputType.TYPE_NULL;
175.
176.
177. public boolean onKeyDown(View view, Editable content,
178. int keyCode, KeyEvent event)
179. return false;
180.
181.
182. public boolean onKeyUp(View view, Editable content, int keyCode,
183. KeyEvent event)
184. return false;
185.
186.
187. public boolean onKeyOther(View view, Editable content, KeyEvent event)
188. return false;
189.
190.
191. public void clearMetaKeyState(View view, Editable content, int states)
192.
193.
194. public static NullKeyListener getInstance()
195. if (sInstance != null)
196. return sInstance;
197.
198. sInstance = new NullKeyListener();
199. return sInstance;
200.
201.
202. private static NullKeyListener sInstance;
203.
204.
205. public void release()
206. if (mResolver != null)
207. final ContentResolver contentResolver = mResolver.get();
208. if (contentResolver != null)
209. contentResolver.unregisterContentObserver(mObserver);
210. mResolver.clear();
211.
212. mObserver = null;
213. mResolver = null;
214. mPrefsInited = false;
215.
216.
217.
218. private void initPrefs(Context context)
219. final ContentResolver contentResolver = context.getContentResolver();
220. mResolver = new WeakReference<ContentResolver>(contentResolver);
221. if (mObserver == null)
222. mObserver = new SettingsObserver();
223. contentResolver.registerContentObserver(Settings.System.CONTENT_URI, true, mObserver);
224.
225.
226. updatePrefs(contentResolver);
227. mPrefsInited = true;
228.
229.
230. private class SettingsObserver extends ContentObserver
231. public SettingsObserver()
232. super(new Handler());
233.
234.
235. @Override
236. public void onChange(boolean selfChange)
237. if (mResolver != null)
238. final ContentResolver contentResolver = mResolver.get();
239. if (contentResolver == null)
240. mPrefsInited = false;
241. else
242. updatePrefs(contentResolver);
243.
244. else
245. mPrefsInited = false;
246.
247.
248.
249.
250. private void updatePrefs(ContentResolver resolver)
251. boolean cap = System.getInt(resolver, System.TEXT_AUTO_CAPS, 1) > 0;
252. boolean text = System.getInt(resolver, System.TEXT_AUTO_REPLACE, 1) > 0;
253. boolean period = System.getInt(resolver, System.TEXT_AUTO_PUNCTUATE, 1) > 0;
254. boolean pw = System.getInt(resolver, System.TEXT_SHOW_PASSWORD, 1) > 0;
255.
256. mPrefs = (cap ? AUTO_CAP : 0) |
257. (text ? AUTO_TEXT : 0) |
258. (period ? AUTO_PERIOD : 0) |
259. (pw ? SHOW_PASSWORD : 0);
260.
261.
262. /* package */ int getPrefs(Context context)
263. synchronized (this)
264. if (!mPrefsInited || mResolver.get() == null)
265. initPrefs(context);
266.
267.
268.
269. return mPrefs;
270.
以上源码中真正使用到autoText的是getInputType()这个方法。在看此方法调用的是makeTextContentType(mAutoCap, mAutoText),这个方法中其实还是在进行inputType的赋值。
三、看到这里,发现autoText和capitalize属性如影随形。而且感觉autoText没有什么用,真正跟autoText有关系的就是下面这句
[java] view plain copyprint?
1. if (autoText)
2. contentType |= InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;//自动更正文本输入
3.
官方描述为“自动更正文本输入”。既然是更正,所以必须要有规则。没有规则就不知道什么是正确的。所以单独使用autoText是不起任何作用的。需要和capitalize配合使用。
四、最后,总结一下。
1、autoText是作用在EditText上的属性。
2、即使autoText和capitalize配合使用,他们也是在给inputType赋值。那我们为什么不直接使用inputType这个属性呢—–其实android官方也是建议直接使用inputType这个属性的。不管是使用autoText和capitalize或者inputType,其实只是对手机输入法软键盘有一些影响。EditText本身不会对setText的内容做什么调整。
如果你还是在使用autoText和capitalize这2个属性,最好使用inputType替换。不然出现如下警告:
Deprecated views, attributes and so on are deprecated because there is a better way to do something. Do it that new way. You’ve been warned
通过警告也可以看到,autoText和capitalize已经过时了。
去记录一个过时的方法,感觉有点“然而这并没有什么卵用”的感觉。只是自己在看到这个属性时,不知道这个属性在干什么。所以当我弄明白的时候,就记录于此
Android - 控件android:ems属性
(转载自:http://blog.csdn.net/caroline_wendy/article/details/41684255)
xml中android:ems属性,作为EditText默认生成的属性,其含义是需要编辑的字符串长度。
设置为10时,最多编辑10个em,一个em单位是两个inch,但是随着自动调整,在Android中em代表‘M’的数量。
但是EditText的属性,只有在android:layout_width=“wrap_content”时,才会显示;
如果是android:layout_width=“match_parent”时,则不会有变化。
em参考:
http://www.w3.org/Style/Examples/007/units
“The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion. Declarations such as ‘text-indent: 1.5em’ and ‘margin: 1em’ are extremely common in CSS.”
ems属性参考:
参考:http://stackoverflow.com/questions/7053738/what-is-meant-by-ems-android-textview
android:ems or setEms(n) sets the width of a TextView to fit a text of n ‘M’ letters regardless of the actual text extension and text size. See wikipedia Em unit. but only when the layout_width is set to “wrap_content”. Other layout_width values override the ems width setting.
Adding an android:textSize attribute determines the physical width of the view to the textSize * length of a text of n ‘M’s set above.
我的Android进阶之旅——>Android 标签的用法
(转载自:http://blog.csdn.net/ouyang_peng/article/details/46957281)
布局资源文件的根节点可以使用容器控件(如LinearLayout、FrameLayout等),也可以使用非容器控件(如:EditText、TextView等)。对于非容器控件,只能在非容器控件标签中放< requestFocus />标签,表示将当前控件设为焦点。如下代码:
1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. android:layout_width="match_parent"
3. android:layout_height="match_parent"
4. android:orientation="vertical" >
5.
6. <Button
7. android:id="@+id/buttonRingtone"
8. android:layout_width="wrap_content"
9. android:layout_height="wrap_content"
10. android:text="设置来电铃声" />
11.
12. <Button
13. android:id="@+id/buttonAlarm"
14. android:layout_width="wrap_content"
15. android:layout_height="wrap_content"
16. android:text="设置闹钟铃声" />
17.
18. <Button
19. android:id="@+id/buttonNotification"
20. android:layout_width="wrap_content"
21. android:layout_height="wrap_content"
22. android:text="设置通知铃声" />
23.
24. <EditText
25. android:id="@+id/text"
26. android:layout_width="match_parent"
27. android:layout_height="wrap_content" >
28. <!-- 当前控件处于焦点状态 -->
29. <requestFocus />
30. </EditText>
31. </LinearLayout>
运行后,EditText控件处于焦点状态,光标一直在动,
以上是关于Android 零碎知识点---2的主要内容,如果未能解决你的问题,请参考以下文章