Android常用的简单代码

1. 数据库SQLite操作

import android.database.Cursor; import android.database.sqlite.SQLiteCursorDriver; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQuery; import android.database.sqlite.SQLiteDatabase.CursorFactory;//创建数据库,openOrCreateDatabase为Context(Activity继承Context)中方法 SQLiteDatabase myDataBase=this.openOrCreateDatabase("myData.db", MODE_PRIVATE, new CursorFactory() { @Override public Cursor newCursor(SQLiteDatabase arg0, SQLiteCursorDriver arg1, String arg2, SQLiteQuery arg3) { return null; } }); //删除数据库,Context.delteDatabase(String) this.deleteDatabase(DBName); //执行非查询SQL指令 String sql="insert into test(id) values(i)"; db.execSQL(sql); //执行查询指令 Cursor cur=db.rawQuery("select * from test", null); if(cur!=null)//游标初始指向第一行 { //得到id所在列 int numColumn=cur.getColumnIndex("id"); if(cur.moveToFirst()){ do{ int val=cur.getInt(numColumn);//得到id值 }while(cur.moveToNext()); } }

2.获得屏幕信息

import android.util.DisplayMetrics; //描述显示信息的结构:密度、高、宽、字体尺寸 DisplayMetrics displayMetrics=new DisplayMetrics(); //初始化DisplayMetrics getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int width=displayMetrics.widthPixels; int height=displayMetrics.heightPixels;

3. 提示信息

import android.app.AlertDialog; //提示对话框 AlertDialog.Builder dialog=new AlertDialog.Builder(this); dialog.setTitle("提示"); dialog.setMessage("android提示"); dialog.show(); //提示短消息,无交互 import android.widget.Toast; //makeText(Context,CharSequence,int),int参数为显示多长时间 //系统带的有Toast.LENGTH_LONG,Toast.LENGTH_SHORT Toast.makeText(HelloActivity.this, "android小提示", Toast.LENGTH_LONG).show();

4.Activity间跳转

//起始Activity设置 Intent next_intent=new Intent(); //Activity间数据传递 Bundle bundle=new Bundle(); bundle.putString("username", editText.getText().toString()); next_intent.putExtras(bundle); //指定跳转 next_intent.setClass(HelloActivity.this, NextPageActivity.class); startActivity(next_intent); //目的Activity获取数据 Bundle bundle=this.getIntent().getExtras(); String username=bundle.getString("username");

5.跳转到指定网站

import android.net.Uri; //打开网页 Uri uri=Uri.parse("http://www.baidu.com"); Intent intent=new Intent(Intent.ACTION_VIEW,uri); startActivity(intent);

待续

 

你可能感兴趣的:(数据库,android,String,sqlite,null,import)