这些技巧都适用androidsdk 1.0
1.在测试时,如何实现一个提示
可以使用
-
Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show();
-
//从资源文件string.xml 里面取提示信息
-
Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show();
这个提示会几秒钟后消失
2.可以使用AlertDialog.Builder才产生一个提示框.
例如像messagebox那样的
-
new AlertDialog.Builder(this)
-
.setTitle("Android 提示")
-
.setMessage("这是一个提示,请确定")
-
.show();
带一个确定的对话框
-
new AlertDialog.Builder(this)
-
.setMessage("这是第二个提示")
-
.setPositiveButton("确定",
-
new DialogInterface.OnClickListener(){
-
public void onClick(DialogInterface dialoginterface, inti){
-
//按钮事件
-
}
-
})
-
.show();
AlertDialog.Builder还有很多复杂的用法,有确定和取消的对话框
-
new AlertDialog.Builder(this)
-
.setTitle("提示")
-
.setMessage("确定退出?")
-
.setIcon(R.drawable.quit)
-
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int whichButton) {
-
setResult(RESULT_OK);//确定按钮事件
-
finish();
-
}
-
})
-
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int whichButton) {
-
//取消按钮事件
-
}
-
})
-
.show();
3.menu 的用法.
-
public static final int ITEM_1_ID = Menu.FIRST;
-
public static final int ITEM_2_ID = Menu.FIRST + 1;
-
public static final int ITEM_3_ID = Menu.FIRST + 2;
-
-
public boolean onCreateOptionsMenu(Menu menu) {
-
super.onCreateOptionsMenu(menu);
-
//不带图标的menu
-
menu.add(0, ITEM_1_ID, 0, "item-1");
-
//带图标的menu
-
menu.add(0, ITEM_2_ID, 1, "item-2").setIcon(R.drawable.editbills2);
-
menu.add(0, ITEM_3_ID, 2, "item-3").setIcon(R.drawable.billsum1);
-
return true;
-
}
-
-
public boolean onOptionsItemSelected(MenuItem item){
-
switch (item.getItemId()) {
-
case 1:
-
Toast.makeText(this, "menu1",Toast.LENGTH_SHORT).show();
-
return true;
-
case 2:
-
-
return true;
-
case 3:
-
-
return true;
-
}
-
return false;
-
}
4.Activity的切换
2个Activity的切换,没有数据传递
-
//从A到B
-
Intent intent = new Intent();
-
intent.setClass(A.this, B.class);
-
startActivity(intent);
2个Activity之间传递数据
相关的几个函数
startActivityForResult
public final void setResult(int resultCode, String data)
回调函数
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data)
例如A到B,从B得到数据
-
//A到B
-
static final int RG_REQUEST = 0;
-
Intent intent = new Intent();
-
intent.setClass(A.this, B.class);
-
startActivityForResult(intent,RG_REQUEST);
-
-
//在B中处理
-
Bundle bundle = new Bundle();
bundle.putString("DataKey", edittext.getText().toString());//给bundle 写入数据
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish(); -
-
//最后在A的回调函数里面接收数据
-
if (requestCode == RG_REQUEST) {
if (resultCode == RESULT_CANCELED)
setTitle("Canceled...");
else if(resultCode == RESULT_OK) {
setTitle((String)data.getCharSequenceExtra("DataKey"));
}
}