Android基本概念小整理(二)
1.SQLite
圖形化界面來查看數(shù)據(jù)庫,使用Sqliteman
>sudo apt-get install sqliteman
2.關于Activity間的跳轉
>1.Intent it = getIntent();
>2.Intent it = new Intent();
說說第一種,假設注冊了個單擊跳轉的事件,第一次點擊是有反應的,現(xiàn)在退出整個應用程序,再次啟動,此時的單擊事件都失效,其他的監(jiān)聽事件都沒問題,就屬單擊事件.
3.關于ListView 和 ExpandableListView這種列表控件
Java代碼
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
String title = ((TextView) info.targetView.findViewById(R.id.groupName)).getText().toString();
menu.setHeaderTitle(title);
menu.add(0, MENU_GROUP_ADD, 0, "添加分組");
menu.add(0, MENU_GROUP_DELETE, 0, "刪除分組");
menu.add(0, MENU_GROUP_MODIFY, 0, "重命名");
menu.add(0, MENU_GROUP_ADDCONTACT, 0, "添加聯(lián)系人");
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
String title = ((TextView) info.targetView.findViewById(R.id.chats_view_name)).getText().toString();
menu.setHeaderTitle(title);menu.add(0, MENU_CONTACTS_DELETE, 0, "刪除聯(lián)系人");
menu.add(0, MENU_CONTACTS_MODIFY, 0, "編輯聯(lián)系人");
menu.add(0, MENU_CONTACTS_MOVE, 0, "移動聯(lián)系人到...");}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo menuInfo = (ExpandableListContextMenuInfo) item.getMenuInfo();
int type = ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
String groupName = ((TextView) menuInfo.targetView.findViewById(R.id.groupName)).getText().toString();
Log.i(TAG, groupName);
switch (item.getItemId()) {
case MENU_GROUP_ADD:break;
case MENU_GROUP_DELETE:break;
case MENU_GROUP_MODIFY:break;
case MENU_GROUP_ADDCONTACT:break;
}
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
String childName = ((TextView) menuInfo.targetView.findViewById(R.id.chats_view_name)).getText().toString();
Log.i(TAG, childName);
switch (item.getItemId()) {
case MENU_CONTACTS_DELETE:break;
case MENU_CONTACTS_MODIFY:break;
case MENU_CONTACTS_MOVE:break;
}
}
return true;
}
如果需要給group或者child 加上個長按事件
這樣寫了,沒反應,需要將實列注冊給監(jiān)聽器
Java代碼
ExpandableListView mElv = (ExpandableListView) findViewById(android.R.id.list);registerForContextMenu(mElv);
另外需要提到一點的是,我在項目里注冊了長按事件,同時group下的child也有個onClick()事件,這時候,onClick()事件無效
需要給child增加單擊事件
Java代碼
mElv.setOnChildClickListener(mChildClickListener);// 注冊group下的item的點擊事件
OnChildClickListener mChildClickListener = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Main.this, ChatActivity.class);
startActivity(intent);
return false;
}
};
> 今天發(fā)現(xiàn) 想要將列表控件中間的分割符去掉,可以通過android:divider="@null"來設置.
別走開 ,下頁繼續(xù)深入分析
#p#
4.讓控件顯示在底部(不是界面上的底部),也就是從底部開始顯示,可以采用android:layout_gravity="bottom",該控件不能放在相對布局里,否則沒效果
5.想做出個跟CSS里的Float效果,可以采用RelativeLayout的嵌套,在子RelativeLayout里設置android:layout_alignParentRight="true"等一系列屬性.
6.ListView中如何使用Button,讓onClick和onItemClick事件共存,可以在布局文件里,將這個Button添加個android:focusable="false"屬性
7.EditText控件,讓光標停在字符最後
Java代碼
EditText inputField = new EditText(this);
Editable eText = inputField.getText();
int position = eText.length();
Selection.setSelection(eText, position);
8.讓控件均勻分布,整齊的排列
Java代碼
<LinearLayout android:id="@+id/chat_bottom"
android:layout_width="fill_parent" android:layout_height="50dip"
android:orientation="horizontal" android:visibility="gone">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1.0"
android:layout_gravity="center_vertical">
<Button android:id="@+id/btn_chat_history"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/chat_history_button"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1.0"
android:layout_gravity="center_vertical">
<Button android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/widget_qq"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1.0"
android:layout_gravity="center_vertical">
<Button android:id="@+id/back_chats_view"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/back_btn"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
9.有時候發(fā)現(xiàn)你界面上有個EditText控件,真機上進入這個界面,會獲得焦點,然后彈出一個軟鍵盤出來,可以做如下處理,將焦點給轉移.
Java代碼
- <!-- 使EditText的焦點移到linearlayout上,同時保證EditText還可以獲得焦點 -->
- <LinearLayout android:focusable="true"
- android:focusableInTouchMode="true" android:layout_height="0px"
- android:layout_width="0px">
- </LinearLayout>
10.控制軟件盤的顯示與隱藏
Java代碼
- /**
- * 控制軟鍵盤的顯示與隱藏
- */
- private void opSoftInput(View view, boolean hasFocus){
- if(hasFocus){
- putMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(view, 0);
- } else {
- putMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
- }
- }
11.上次寫了篇關于TextView里解析包含圖片的博客,這次去弄了下EditText中,點擊表情,將表情圖片添加到EditText (類似QQ表情的發(fā)送吧)
呵呵,中間走了點彎路,開始是將EditText中的內容拿去解析,還用了正則去匹配,結果失敗,
SpannableString類的setSpan(Object what, int start, int end, int flags)
我采用map保存了表情,
看下當時寫的重寫的EditText的setText()方法
Java代碼
- @Override
- public void setText(CharSequence text, BufferType type) {
- // TODO Auto-generated method stub
- initFaceMap();
- // 需要處理的文本
- SpannableString spannable = new SpannableString(text.toString());
- if (text != null && faceMap != null) {
- // 對表情符以img標記進行修飾,改用drawable顯示出來
- Set<String> keys = faceMap.keySet();
- for (String key : keys) {
- if (text.toString().contains(key)) {
- Drawable drawable = getResources().getDrawable(faceMap.get(key));
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- //要讓圖片替代指定的文字就要用ImageSpan
- ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
- int start = text.toString().indexOf(key);
- spannable.setSpan(span, start, start + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
- }
- }
- }
- super.setText(spannable, type);
- }
這樣寫,如果有輸入相同的表情,則只有第一個顯示正常,后續(xù)的都是字符,因為我只遍歷了一遍map,而且是替換了一遍.
下面是簡單方法(需要在布局文件里給每個表情按鈕添加一個tag屬性,展示的是表情,實質內容還是tag的文本,方便接收方的解析)
Java代碼
- /** 聊天表情圖片監(jiān)聽器 */
- private OnClickListener faceImageButtonListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- ImageButton imageButton = (ImageButton) v;
- String emotionTag = imageButton.getTag().toString();
- int cursor = content.getSelectionEnd();
- content.getText().insert(cursor, emotionTag);
- SpannableString spannable = new SpannableString(content.getText());
- Drawable draw = imageButton.getDrawable();
- ImageSpan span = new ImageSpan(draw, ImageSpan.ALIGN_BASELINE);
- // 下面flags參數(shù)換成Spannable.SPAN_INCLUSIVE_EXCLUSIVE,會導致添加了表情后,無法刪除表情,也無法輸入字符
- spannable.setSpan(span, cursor, cursor + emotionTag.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- content.setText(spannable);
- Editable eText = content.getText();
- Selection.setSelection(eText, content.getText().length());// 控制光標顯示在字符最后
- }
- };
最后貼上效果圖