1.SQLite
创建数据库
Android 不自动提供数据库。在 Android 应用程序中使用 SQLite,必须自己创建数据库,然后创建表、索引,填充数据。Android 提供了 SQLiteOpenHelper 帮助你创建一个数据库,你只要继承 SQLiteOpenHelper 类,就可以轻松的创建数据库。SQLiteOpenHelper 类根据开发应用程序的需要,封装了创建和更新数据库使用的逻辑。SQLiteOpenHelper 的子类,至少需要实现三个方法:
- 构造函数,调用父类 SQLiteOpenHelper 的构造函数。这个方法需要四个参数:上下文环境(例如,一个 Activity),数据库名字,一个可选的游标工厂(通常是 Null),一个代表你正在使用的数据库模型版本的整数。
- onCreate()方法,它需要一个 SQLiteDatabase 对象作为参数,根据需要对这个对象填充表和初始化数据。
- onUpgrage() 方法,它需要三个参数,一个 SQLiteDatabase 对象,一个旧的版本号和一个新的版本号,这样你就可以清楚如何把一个数据库从旧的模型转变到新的模型。
SQLiteOpenHelper 的子类调用 getReadableDatabase() 或 getWriteableDatabase() 方法,你可以得到 SQLiteDatabase 实例。
使用SQLiteDatabase 对象的 insert(), update(), delete(),rawQuery/query 方法,进行增删改查。
Cursor---查询结果集
用StringBuilder写Sql
例: StringBuilder selectBuilder = new StringBuilder();
selectBuilder.append("select * from ").append(DBHelper.TABLE_NAME).append(" where ")
.append(DBHelper.COLUMN_DATE).append(" =?").append(" order by ").append(DBHelper.COLUMN_TYPE);
2.AlertDialog
new AlertDialog.Builder(this)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
)
.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
}
})
.setNegativeButton("取消", null)
.setNeutralButton("Neutral2", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.cancel();
}
})
.show();
3.Toast
Toast.makeText( ToastActivity.this,“hello”,Toast.LENGTH_LONG).show();
4.spinner
spinner = (Spinner) findViewById(R.id.spinType);
spinner.setAdapter(adapter);
//给弹出的选择框加一个标题
spinner.setPrompt("选择类型");
其中adapter:
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, String[] );
或adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, ArrayList<String>);
或adapter=new ArrayAdapter<MyEnum>(this,android.R.layout.simple_spinner_item;
adapter.add(MyEnum.xxx);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayList<String>形式的adapter,可以动态删减下拉框中的内容,adapter.add,adapter.remove
5.Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/*添加三个菜单项目,并设置图片*/
menu.add(0, 1, 1, "Edit").setIcon(android.R.drawable.ic_menu_edit);
menu.add(0, 2, 2, "Open").setIcon(android.R.drawable.ic_menu_agenda);
menu.add(0, 3, 3, "Exit").setIcon(android.R.drawable.ic_lock_power_off);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
/*显示main.xml为主屏布局*/
setLayoutShow(R.layout.main);
NoteDebug("编辑文件Layout");
break;
case 2:
/*显示open.xml为主屏布局*/
setLayoutShow(R.layout.open);
NoteDebug( "打开文件Layout");
break;
case 3:
/*退出*/
finish();
NoteDebug( "Byebye");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void NoteDebug(String showString){
/*显示Toast提示*/
Toast.makeText(this,showString, Toast.LENGTH_SHORT ).show();
}
------------------
MenuItem exit,testItem;
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/*添加退出菜单*/
exit=menu.add("Exit");
/*设置退出菜单图片*/
exit.setIcon(android.R.drawable.ic_menu_close_clear_cancel );
testMenuItem = menu.add("testItem");
testMenuItem.setIcon(android.R.drawable.ic_menu_call);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
/*结束Activity*/
if(item.equals(exit)){
finish();
}
else if(item.equals(testMenuItem)){
Toast.makeText(this, "test menuitem is clicked", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
6.TabView
private TabHost tabHost;
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("today").setIndicator("今日账单",getResources().getDrawable(R.drawable.tab_today))
.setContent(new Intent(this,Today.class)
//刷新
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec("history").setIndicator("历史账单",getResources().getDrawable(R.drawable.tab_history))
.setContent(new Intent(this,History.class)));
tabHost.setOnTabChangedListener(this);
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("history"))
{
Intent intent = new Intent(this,SelectDate.class);
startActivityForResult(intent, REQUEST_QUERYDATE);
}
}
7.日期、日历
Calendar calendar = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String today = format.format(calendar.getTime());
8.ListView
String []name=new String[]{"Java","C++","C","C#","VB","XML",".NET","J#"};
ArrayAdapter<String> arrayadapter =new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,name);
listview = (ListView)findViewById(R.id.list);
listview.setAdapter(arrayadapter);
listview.setOnItemClickListener(this);
------复杂点的
listview.setAdapter(new ListItemAdapter(this));
class ListItemAdapter extends BaseAdapter
{};
this.layoutInflater = LayoutInflater.from(context);
view=layoutInflater.inflate(R.layout.listview_item, null); ----inflate一个新的布局
9.Html.fromHtml
Spanned spannable = Html.fromHtml("the google url: " +
"<a href=\"http://www.google.com\">http://www.google.com</a><br/>" +
"the telephone: " +
"<a href=\"tel:18603045201\">18603045201</a>"
);
textview.setText(spannable);
textview.setMovementMethod(LinkMovementMethod.getInstance());//点击链接可以直接访问网页或者打电话
SpannableString ss=
new SpannableString("the google url: http://www.google.com 18600000001");
ss.setSpan(new URLSpan("http://www.google.com"),
16, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:18603045201"),
38, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(ss);
textview.setMovementMethod(LinkMovementMethod.getInstance());