整理出来的一些Android基础知识:
==============
在application 节点配置首先启动的Activity 节点
- <activity android:name=".HelloActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
=============
获取当前屏幕的宽和高
- WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
- Display display = manager.getDefaultDisplay();
- Display display = manager.getDefaultDisplay();
- this.screen_width = display.getWidth();
- this.screen_height = display.getHeight();
=============
win7 磨砂效果
- Window window = dialog.getWindow();
- window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
- WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
=============
设置屏幕默认为横屏
- android:screenOrientation="landscape"
设置屏幕默认为竖屏
- android:screenOrientation="portrait"
=============
去除窗体的标题栏
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
去掉任务栏
- this.getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
这个方法必须放在以上两个方法之后
- this.setContentView(layoutResID);
================
ContentProvider 使用表的形式来组织数据
各个程序都可以来访问这个数据
android中的ContentProvider 保存在android.provider中
每个ContentProvider 都有一个uri
提供的增删改查的函数方法:
主要意义在于提供在不同的程序之间实现数据共享
实现一个CongtentProvider的步骤
1、定义一个CONTENT_URI常量
2、定义一个类,继承ContenProvider
3、实现query 、insert、 update 、delete、getType 和onCreate方法
4、在AndroidManifest.xml 当中声明
================
Activity 之间的跳转
Activity01 中启动Activity02
- Intent inetnt1 = new Intent(Activity01.this,Activity2.calss)
- stratActivity(intent1);
- startActivityForResult(intent1,REQUEST_CODE);
startActivityForResult() 这个方法启动intent,并且挡墙新的Activity 运行结束的时候,还必须执行原Activity 里面的回调函数onActivityResult().
================
关于权限问题
===============
指定layout
- LayoutInflater factory = LayoutInflater.from(NotePad.this);
==================
响应手机返回按钮事件
- @Override
- public void onBackPressed() {
- AlertDialog.Builder builder = new AlertDialog.Builder(ManagerView.this);
- builder.setMessage("是否退出程序");
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- ManagerView.this.finish();
- }
- });
- builder.show();
-
- }
================
响应MENU按钮创建菜单
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
-
- MenuItem item_0 = menu.add(0, 0, 0, "EXIT");
- item_0.setOnMenuItemClickListener(new ItemClickListenerClass());
- MenuItem item_1 = menu.add(0, 1, 1, "About");
- item_1.setOnMenuItemClickListener(new ItemClickListenerClass());
- MenuItem item_2 = menu.add(0, 2, 2, "UPdate");
- item_1.setOnMenuItemClickListener(new ItemClickListenerClass());
- MenuItem item_3 = menu.add(0, 3, 3, "Connect US");
- item_1.setOnMenuItemClickListener(new ItemClickListenerClass());
- return super.onCreateOptionsMenu(menu);
- }
=================
上下文菜单
创建
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- menu.setHeaderTitle("选项");
- menu.setHeaderIcon(R.drawable.menu_set);
-
- menu.add(0, 0, 0, "新建记事项");
- menu.add(0, 1, 1, "保存记事");
- menu.add(0, 2, 2, "清空当前");
- menu.add(0, 3, 3, "查看记事");
-
- super.onCreateContextMenu(menu, v, menuInfo);
- }
-
-
-
-
-
-
-
-
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
- .getMenuInfo();
- switch (item.getItemId()) {
- case 0:
- break;
-
- }
- return super.onContextItemSelected(item);
- }
==================
对listview 中我们可以调用Android中默认的布局模式
Android.R.layout.simple_list_item_1 每一项只有一个textview
Android.R.layout.simple_list_item_2 每一项有两个TextView
Android.R.layout.simple_list_item_single_choice 每一项有一个TextView 但是这个项可以被选择
=================
Dialog 类是继承的是 object
progressDialog 中不需要Builder 这个内部类来构造,可以直接调用构造函数
=================
设置显示字体为系统里面的字体
- Android:textAppearance="?Android:attr/textApperanceMedium"
====================
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
- holder = new ViewHolder();
- holder.text = (TextView) convertView.findViewById(R.id.text);
- holder.icon = (ImageView) convertView.findViewById(R.id.icon);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.text.setText(DATA[position]);
- holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
- return convertView;
- }
-
- static class ViewHolder {
- TextView text;
- ImageView icon;
- }
=====================
关于SQLite 数据库的操作
继承SQLiteOpenHelper类新建一个数据库
- public class DBUtil extends SQLiteOpenHelper {
-
- private static final String SQL_STRING = "create table test_sqlite(_id integer primary key autoincrement,username varchar(40),birthdate date)";
-
- private static final String DB_NAME = "testsqlite.db";
- private static final int DB_VERSION = 1;
-
-
-
-
-
-
-
-
- public DBUtil(Context context, String name, CursorFactory factory,
- int version) {
- super(context, name, factory, version);
-
- }
-
- public DBUtil(Context context) {
- this(context, DB_NAME, null, DB_VERSION);
-
- }
-
-
-
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- System.out.println("onCreate()............");
- db.execSQL(SQL_STRING);
-
- }
-
-
-
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-
- db.execSQL("drop table if exists testsqlite");
- onCreate(db);
- }
-
- public void closeDB(SQLiteDatabase db){
- if(db != null){
- db.close();
- }
- }
-
- public void closeCursor(Cursor c){
- if(c != null){
- c.close();
- }
- }
- }
======================
对于SQLite 数据库的基本操作语句:
插入操作:
对数据库中插入一个USERVO 对象
- public void insertUserVO(UserVO user){
-
- DBUtil util = new DBUtil(context);
-
- SQLiteDatabase db = util.getWritableDatabase();
-
- ContentValues values = new ContentValues();
- values.put("username",user.getUsername());
- values.put("birthdate", ConvertDate.datetoString(user.getBirthdate()));
-
- db.insert("test_sqlite", null, values);
-
-
- }
查询操作:
- public Cursor findAll(){
- DBUtil util = new DBUtil(context);
- SQLiteDatabase db = util.getReadableDatabase();
-
-
- String[] colnames = new String[]{"_id","username","birthdate"};
-
- String selection = "userid = ? and username=?";
- String[] selectionArgs = new String[]{"1","saf"};
- String groupBy = "userid";
- String having = "userid <10";
- String orderBy = "userid asc";
-
- Cursor c = db.query("test_sqlite", colnames, null, null, null, null, null);
- while(c.moveToNext()){
- System.out.println(c.getColumnName(0)+" "+c.getColumnName(1)+" "+c.getColumnName(2));
- System.out.println(c.getInt(0)+" "+c.getString(1)+" "+c.getString(2));
-
- }
-
- return c;
-
- }
//查询的一些列方法:
-----------
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
distinct: true 表示去掉重复的行
table :表名
colums :查询的列的string 数组
selection:表示查询的条件 包含关键字 where 类似 selection = “name=?”;
selectionArgs :表示 填充的是 ? 代表的值的一个string数组
limit 限制返回的记录条数
-----------
- public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
-----------
- public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
-----------
- public Cursor rawQuery (String sql, String[] selectionArgs)
-----------
//修改数据的方法
- public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
-----------
//删除数据
public int delete (String table, String whereClause, String[] whereArgs)