android整理的一些零散笔记

 

 

 

=>1.SQLite

图形化界面来查看数据库,使用Sqliteman

>sudo apt-get install sqliteman

 

=>2.关于Activity间的跳转

>1.Intent it = getIntent();

>2.Intent it = new Intent();

说说第一种,假设注册了个单击跳转的事件,第一次点击是有反应的,现在退出整个应用程序,再次启动,此时的单击事件都失效,其他的监听事件都没问题,就属单击事件.

 

=>3.关于ListView 和 ExpandableListView这种列表控件

@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, "添加联系人");
		} 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, "删除联系人");
			menu.add(0, MENU_CONTACTS_MODIFY, 0, "编辑联系人");
			menu.add(0, MENU_CONTACTS_MOVE, 0, "移动联系人到...");}
		}
	@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 加上个长按事件

这样写了,没反应,需要将实列注册给监听器

ExpandableListView mElv = (ExpandableListView) findViewById(android.R.id.list);registerForContextMenu(mElv); 


另外需要提到一点的是,我在项目里注册了长按事件,同时group下的child也有个onClick()事件,这时候,onClick()事件无效

需要给child增加单击事件

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;
}
};


> 今天发现 想要将列表控件中间的分割符去掉,可以通过android:divider="@null"来设置.

 

=>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控件,让光标停在字符最後

EditText inputField = new EditText(this);

Editable eText = inputField.getText();

int position = eText.length();

Selection.setSelection(eText, position);
=>8.让控件均匀分布,整齐的排列

<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.有时候发现你界面上有个EditText控件,真机上进入这个界面,会获得焦点,然后弹出一个软键盘出来,可以做如下处理,将焦点给转移.

<!-- 使EditText的焦点移到linearlayout上,同时保证EditText还可以获得焦点 -->
	<LinearLayout android:focusable="true"
		android:focusableInTouchMode="true" android:layout_height="0px"
		android:layout_width="0px">
	</LinearLayout>

=>10.控制软件盘的显示与隐藏

/**
	 * 控制软键盘的显示与隐藏
	 */
	private void opSoftInput(View view, boolean hasFocus){
		if(hasFocus){
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(view, 0);
		} else { 
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
		}
	}

=>11.上次写了篇关于TextView里解析包含图片的博客,这次去弄了下EditText中,点击表情,将表情图片添加到EditText (类似QQ表情的发送吧)

呵呵,中间走了点弯路,开始是将EditText中的内容拿去解析,还用了正则去匹配,结果失败,

SpannableString类的setSpan(Object what, int start, int end, int flags)

我采用map保存了表情,

看下当时写的重写的EditText的setText()方法

@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);
	}
这样写,如果有输入相同的表情,则只有第一个显示正常,后续的都是字符,因为我只遍历了一遍map,而且是替换了一遍.

下面是简单方法(需要在布局文件里给每个表情按钮添加一个tag属性,展示的是表情,实质内容还是tag的文本,方便接收方的解析)

/** 聊天表情图片监听器 */
	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参数换成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());// 控制光标显示在字符最后
		}
	};

最后贴上效果图


=>12.activity A startActivityForResult启动的B,如果A设置了运行模式android:launchMode="singleInstance"之类的,会导致启动B前就调用onActivityResult方法。

=>13.命令操作sdcard

-- 进入sdcard目录
>cd ~/android-sdk-linux/platform-tools
>./adb shell
>ls
>cd mnt
>cd sdcard

-- 安装apk
>cd ~/android-sdk-linux/platform-tools
>./adb install ~/Downloads/cdjm.autoquery.apk

--从sdcard下拉取文件
>./adb pull sdcard/odk/metadata/instances.db

--从PC端copy文件到sdcard下
>./adb push ~/Desktop/xxx.db /sdcard/xxx.db


=>14.apktool

http://code.google.com/p/android-apktool/

Linux:
Download apktool-install-linux-* file
Download apktool-* file
Unpack both to /usr/local/bin directory (you must have root permissions)

> ll /usr/local/bin
> chmod a+w /usr/local/bin
> apktool d -f zhangshangrenbao.apk /home/sushuo/Desktop/a



你可能感兴趣的:(android整理的一些零散笔记)