日志分类:Android应用

sqlite管理工具-Dbvis

时间:2011年06月01日作者:ronald查看次数:414 views评论次数:0

之前我的博文中也已经提到dbvis的通用数据库管理工具.[通用数据库管理工具-Dbvis].上次提到的现在dbvis准备出新版本了,我关于的两个新特性是新增对h2数据库sqlite数据库的支持.
我相信开发android的人员使用的数据库绝大多数都是使用sqlite的,因为android中默认支持.
我之前想过在android中使用derby的,但是考虑这样会增大应用的大小.这对于手机应用来说根本就不能接受.
所以开发android一直都使用sqlite. 但是我发现在windows中没有发现特别好用的管理工具(之前一直使用的dbvis还没有支持)

为什么要在windows中找个sqlite的管理工具呢?我觉得有以下几点是需要的:
1, 准备一些测试数据,方便在应用中进行测试
2, 可能会将一些日志,配置等信息放在sqlite数据库中,需要导出来进行查看数据或修改配置信息

以下是如何连接sqlite的一些中的截图.希望对大家有用.

选择数据库类型为sqlite

指定sqlite数据库的文件路径:

注意:发现该版本的bug,连接后会自动转成h2数据库类型,需要手动改动后才能正常使用

在SQL Commander里执行SQL语句:
[ create table test_table (id numeric(18),name varchar(100)); ]

以下连接成功后,创建了的测试表后的视图,可以看到功能还是比较多的,已经能满足大部分的需求.

现在dbvis7.2版本还在测试中,破解版本还没有出,一般在正式版发布后两个星期左右会有破解文件放出.
现在就先用用测试版本吧
下载地址: http://www.dbvis.com/products/dbvis/eap/install.jsp

返回 : Android开发博文汇总

Android使用自定义AlertDialog(退出提示框)

时间:2011年05月22日作者:ronald查看次数:2,059 views评论次数:1

有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog)
以下是我在开发一个小游戏中总结出来的.希望对大家有用.

先上效果图:

下面是用到的背景图或按钮的图片

经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置alertDialog的contentView.

以下的代码是写在Activity下的,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public boolean onKeyDown(int keyCode, KeyEvent event) {
	// 如果是返回键,直接返回到桌面
	if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
           showExitGameAlert();
	}
 
	return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
	final AlertDialog dlg = new AlertDialog.Builder(this).create();
	dlg.show();
	Window window = dlg.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
	window.setContentView(R.layout.shrew_exit_dialog);
        // 为确认按钮添加事件,执行退出应用操作
	ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
	ok.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			exitApp(); // 退出应用...
		}
	});
 
        // 关闭alert对话框
        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
	  public void onClick(View v) {
		  dlg.cancel();
		}
	  });
}

以下的是layout文件,定义了对话框中的背景与按钮.点击事件在Activity中添加.
文件名为 : shrew_exit_dialog.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content" 
	android:layout_width="wrap_content">
 
	<!-- 退出游戏的背景图 -->
	<ImageView android:id="@+id/exitGameBackground"
		android:layout_centerInParent="true" 
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" 
		android:src="@drawable/bg_exit_game" />
 
	<!-- 确认按钮 -->
	<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
		android:layout_alignLeft="@+id/exitGameBackground"
		android:layout_marginBottom="30dp" 
		android:layout_marginLeft="35dp"
		android:id="@+id/btn_ok" 
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" 
		android:background="@drawable/btn_ok" />
 
	<!-- 取消按钮 -->
	<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
		android:layout_alignRight="@+id/exitGameBackground"
		android:layout_marginBottom="30dp" 
		android:layout_marginRight="35dp"
		android:id="@+id/btn_cancel" 
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" 
		android:background="@drawable/btn_cancel" />
</RelativeLayout>

就这样经过了以上几步,就可以实现自定义AlertDialog的效果了. 用同样的思路可以实现其它更复杂的效果.

返回 : Android开发博文汇总

Android中dip(dp)与px之间单位转换

时间:2011年05月12日作者:ronald查看次数:3,563 views评论次数:0

dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)
但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因为可以支持多种分辨率的手机.

以下是这两个单位的概念:
px (pixels)像素 –一个像素通常被视为图像的最小的完整采样,这个用的比较多,特别是web开发,页面基本都是使用像素作为单位的.
dip或dp (device independent pixels)设备独立像素 — 这个和设备硬件有关,一般我们为了支持手机上多种分辨率,如WVGA、HVGA和QVGA,都会使用dip作为长度的单位

在Android开发我们一般都可以不需要使用px的,但是某一些控件的属性没有直接支持dip,像下面的代码

android.view.ViewGroup.LayoutParams.height
android.view.ViewGroup.LayoutParams.width

上面这两个属性的单位为像素,但是为了兼容多种分辨率的手机,我们需要最好使用dip,时候我们可以调用以下的代码进行转换.

int heightPx= DisplayUtil.dip2px(this, 33);
mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = heightPx;

以上代码可以在我另一篇文章看得到.该功能是设置Tab的高度,单位是像素.以上的单位转换是为了支持多分辨率手机的.
该文章的地址 : [Android中TabHost的使用]

/**
 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
 */
public static int dip2px(Context context, float dpValue) {
	final float scale = context.getResources().getDisplayMetrics().density;
	return (int) (dpValue * scale + 0.5f);
}
 
/**
 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
 */
public static int px2dip(Context context, float pxValue) {
	final float scale = context.getResources().getDisplayMetrics().density;
	return (int) (pxValue / scale + 0.5f);
}

返回 : Android开发博文汇总

Android中TabHost的使用

时间:2011年05月11日作者:ronald查看次数:1,254 views评论次数:0

先上效果图, 该图片是我软件中的TabHost的使用效果:

(这个是没有选中的背景图)

(这个是选中的背景图)

点击不同的Tab后进行切换不同的Tab的效果.

要实现以上的效果要有以下几的步骤:

第一步:在xml布局页面中添加TabHost

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/tabhost" android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	>
	<LinearLayout android:orientation="vertical"
		android:layout_width="fill_parent" 
                android:layout_height="wrap_content">
		<TabWidget android:id="@android:id/tabs"
			android:layout_width="fill_parent" 
                        android:layout_height="wrap_content"
			 />
		<FrameLayout android:id="@android:id/tabcontent"
			android:layout_width="fill_parent" 
                        android:layout_height="fill_parent">
                <!-- 未开始 -->
		<ListView android:id="@+id/textview_notbegin" 
                         android:layout_width="fill_parent"
		         android:layout_height="fill_parent" 
                         android:drawSelectorOnTop="false" 
                         android:dividerHeight="-1px"/>
                <!-- 进行中 -->
		<ListView android:id="@+id/textview_playing" 
                        android:layout_width="fill_parent"
			android:layout_height="fill_parent" 
                        android:drawSelectorOnTop="false" 
                        android:dividerHeight="-1px"/>
                <!-- 已完场 -->
		<ListView android:id="@+id/textview_finish" 
                        android:layout_width="fill_parent"
			android:layout_height="fill_parent" 
                        android:drawSelectorOnTop="false" 
                        android:dividerHeight="-1px"/>
                <!-- 关注 -->
		<ListView android:id="@+id/textview_focus" 
                        android:layout_width="fill_parent"
			android:layout_height="fill_parent" 
                        android:drawSelectorOnTop="false" 
                        android:dividerHeight="-1px"/>
		</FrameLayout>
	</LinearLayout>
</TabHost>

第二步:编写Activity
在Activity的onCreate方法中添加以下代码:

        // mTabHost定义在Activity的属性
	mTabHost = (TabHost) findViewById(R.id.tabhost);
        mTabHost.setup();
 
        LayoutInflater factory = null;
        // 未开始
        factory = LayoutInflater.from(this);
        final View notStartview = factory.inflate(R.layout.tab,null);
        TextView notStartTextView = (TextView)notStartview.findViewById(R.id.tabName);
		notStartTextView.setText(getText(R.string.text_tab_notstart));
		notStartTextView
               .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
 
        // 进行中
        factory = LayoutInflater.from(this);
        final View playingView = factory.inflate(R.layout.tab,null);
        TextView playingTextView = (TextView)playingView.findViewById(R.id.tabName);
        playingTextView.setText(getText(R.string.text_tab_playing));
        playingTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_act));
 
        // 已完场
        factory = LayoutInflater.from(this);
        final View finishView = factory.inflate(R.layout.tab,null);
        TextView finishTextView = (TextView)finishView.findViewById(R.id.tabName);
        finishTextView.setText(getText(R.string.text_tab_finish));
        finishTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
 
        // 关注
        factory = LayoutInflater.from(this);
        final View focusView = factory.inflate(R.layout.tab,null);
        TextView focusTextView = (TextView)focusView.findViewById(R.id.tabName);
        focusTextView.setText(getText(R.string.text_tab_focus));
        focusTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
 
        // 未开始
        mTabHost.addTab(mTabHost.newTabSpec("tab_not_begin")
          .setIndicator(notStartview)
          .setContent(R.id.textview_notbegin));
        // 进行中
        mTabHost.addTab(mTabHost.newTabSpec("tab_playing")
          .setIndicator(playingView)
          .setContent(R.id.textview_playing));  
        // 已完场
        mTabHost.addTab(mTabHost.newTabSpec("tab_finish")
          .setIndicator(finishView)
          .setContent(R.id.textview_finish));  
        // 关注
        mTabHost.addTab(mTabHost.newTabSpec("tab_focus")
          .setIndicator(focusView)
          .setContent(R.id.textview_focus));  
 
        // 修改宽度,作用两个(关注)与三个字(进行中,已完场)占有不同,三个字宽度大一些
        for (int i =0; i < mTabHost.getTabWidget().getChildCount(); i++) {
        	mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = DisplayUtil.dip2px(this, 33);
 
        	if(i == 1 || i== 2){
        		mTabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 18;
        	} else{
        		mTabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 10;
        	}
        }
 
        // 设置当前选中的Tab
        mTabHost.setCurrentTab(1);
        mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.BLACK);
 
        // tab 选中改变时事件
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
		public void onTabChanged(String arg0) {
			for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++){
			       mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tabName)
                                .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
			}
			mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
                        .findViewById(R.id.tabName)
                        .setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_act));
		}
	});

以上中有使用 DisplayUtil.dip2px(this, 33); 的代码,是为了支持多种分辨率的手机的.
相关内容可以查看 [Android中dip(dp)与px之间单位转换]

第三步:定义Tab的内容xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:background="@drawable/tab"
	>
	<TextView android:id="@+id/tabName"
		android:background="@drawable/tab"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:gravity="bottom|center_horizontal"
		android:textColor="#FFFFFF" 
		android:textSize="14dip"
		android:paddingBottom="3dip"
		android:layout_alignParentBottom="true"
		 />
</RelativeLayout>

以上定义了显示的文字(进行中,已完场…)和背景图片.
TabHost在Android中经常都要使用,了解这种方式可以实现很多的需求了.本人一直都在使用这种方式

返回 : Android开发博文汇总