3个一小时-学会Widget小组件开发,学会各种Drawable,学会SQLite
点击上方 “黄广达” ,选择 “置顶公众号”
有趣有内涵的文章第一时间送达!
喝酒I创作I分享
生活中总有些东西值得分享
前言
大家好,我是 Vic
,今天给大家带来Android开发工程师文集-1 小时学会Widget小组件开发
的概述,希望你们喜欢
学会用Widget (小组件)
Widget小组件很方便,很快捷,可以个性化,自己定制,相关功能,可以实时更新最新内容。
Widget布局,Widget配置,AppWidgetProvider与Configuration Activity
Android Widget
xml布局类型:AppWidget Provider
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
//引用的布局文件
android:initialLayout="@layout/widget"
android:minHeight="50dp"
android:minWidth="100dp"
//设置更新时间
android:updatePerioMillis="66666666">
</appwidget-provide>
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(Context context,int[] appWidgetIds){
super.onDeleted(context,appWidgetIds);
}
@Override
public void onDisabled(Context context){
super.onDisabled(context);
}
@Override
public void onEnabled(Context context){
super.onEnabled(context);
}
@Override
public void onReceive(Context context,Intent intent){
super.onReceive(context,intent);
}
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){
super.onUpdate(context,appWidgetManager,appWidgetIds);
}
}
//TimerService extends Service
Timer timer=new Timer();
timer.shedule(new TimerTask(){
@Override
public void run(){
updateTime();
}
},0,1000);
}
//onDestroy
public void onDestroy(){
super.onDestroy();
timer=null;
}
//updateTime();
private void updateTime(){
String time = simpletime.format(new Date());
}
Widget就是个桌面小组件,有了Widget,我们可以直接在桌面上进行各种操作。
Widget使用
AndroidManifest中声明AppWidget
xml中定义AppWidget配置文件
layout中定义Widget布局文件
创建类继承AppWidgetProvider类
//清单文件中
<meta-data
android:name="android.appwidget.provider"
android:resource="@layout/widget_setting"/>
//onUpdate()重点
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
//Intent intent = new Intent();
Intent intent = new Intent();
intent.setClass(context, TestWidget.class);
intent.setAction(WIDGET_BTN_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_btn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
//清单文件
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider"/>
前言
大家好,我是 Vic
,今天给大家带来Android开发工程师文集-1 小时学会各种Drawable
的概述,希望你们喜欢
Drawable介绍
Drawable为抽象类,drawable与view有区别的地方,主要是体现事件和交互性上面。
view是面向用户的,是可见的控件,能给它添加点击事件。drawable则相反,不能面向,也不可见,也不能添加点击事件。
drawable有好多子类,drawable为抽象类,所以有很多子类来继承它。
主要原理方法
draw()
setBounds()
BitmapDrawable 巴特美drawable
该子类为bitmap的包装,可以保持原图片大小,填充,拉伸。
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_launcher"
android:tileMode="mirror"//平铺
android:antialias="true"//消除锯齿
android:dither="true">
</bitmap>
LayerDrawable 勒儿drawable
该子类就是来管理一组drawable的,第一张图片的放置,那么第二张图片会在第一张图片上,由此依次放置。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="40dp"
android:left="40dp"
android:src="@drawable/picture_1"
android:gravity="center">
</item>
<item
android:top="40dp"
android:left="40dp"
android:src="@drawable/picture_2"
android:gravity="center">
</item>
</layer-list>
StateListDrawable
该类StateListDrawable根据不同状态提供不同图片。
<?xml version="1.0" encoding="utr-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/botton_1"/>
<item android:state_pressed="true" android:drawable="@drawable/botton_2"/>
<item android:state_selected="true" android="@drawable/botton_3"/>
<item android:drawable="@drawable/botton_4"/>
</selector>
LevelListDrawable
该子类有不同的item -setlevel();
<level-list
xmlns:android="http://schemas.android.om/apk/res/android">
<item
android:drawable="@drawable/off"
android:minLevel="5"
android:maxLevel="10">
</item>
<item
android:drawable="@drawable/on"
android:minLevel="10"
android:maxLevel="20">
</item>
</level-list>
public void On(View v){
iv.setImageLevel( //int );
}
TransitionDrawable --- transi tion drawable
为LayerDrawable的子类,只有两张图片的变化,没有多个图片,LayerDrawable可以有多个item,而transitiondrawable只有两个item。
<transition
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/off">
</item>
<item android:drawable="@drawable/on">
</item>
</transition>
public void On(View v){
TransitionDrawable drawable=iv.getDrawable();
//开启 startTransition();
drawable.startTransition(2000);
}
public void Off(View v){
TranstionDrawable drawable=iv.getDrawable();
//关闭 reverseTransition();
drawable.reverseTransition(2000);
}
InsertDrawable
InsertDrawable表示嵌入图片==padding
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/background"
android:insetLeft="50dp"
android:insetRight="50dp"
android:insetTop="50dp"
android:insetBottom="50dp">
</inset>
ClipDrawable 可累drawable
该ClipDrawabel是一种裁剪的形式,如同进度条。
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/picture"
android:clipOrientation="horizontal"
android:gravity="left"/>
ClipDrawable drawable=iv.getDrawable();
drawable.setLevel(5000);
//范围0-10000;
DIY的Drawable
自己动手试试吧
梳理:
BitmapDrawable,
LayerDrawable,
StateListDreawable,
LevelListDrawable,
TransitionDrawable,
InsertDrawable,
ClipDrawable.
前言
大家好,我是 Vic
,今天给大家带来Android开发工程师文集-1 小时学会SQLite
的概述,希望你们喜欢
内容
什么是Sqlite:
效率高,开源,小型,程序驱动,支持事务操作,无数据类型,可嵌入的关系型数据库
独立的,跨平台的,代码量少,简单易用
创建表语句
create table student(_id Integer primary key, name varchar(10), age Integer not null);
删除表
drop table student;
插入数据
Insert into 表名(字段列表) values (值列表);
insert into student(_id,age) values(1,17);
insert into student values(1,"vic",17);
修改数据
update student set name="vic",age=17 where _id=1;
更新数据
Update 表名 set 字段=值 列表 更新的条件
删除数据
delete from 表名 [删除条件];
delete from student where _id=1;
查询语句
select 字段名 from 表名称 [查询条件];
select 列名称 from 表名称 where 条件;
group by 分组的字段 having 筛选条件 order by 排序字段;
select * from student;
select _id from student;
select * from student where _id=1;
select * from student where _id=1 and age>17;
select * from student where age like "%1%";
select * from student where age>17 order by _id=1;
内容
创建数据库
实现数据库中的增删改查
要点
SQLiteOpenHelper
,onCreate()
,onUpgrade()
,onOpen()
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建"
android:onClick="create"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
}
//创建一个类
//sqliteOpenHelper
//提供了onCreate()和onUpgrade()与onOpen()
public class MySqliteHelper extends SQLiteOpenHelper{
public MySqliteHelper(Context context, String name, SQLitebase.CursorFactory factory, int version){
super(context,name,factory,version);
}
public MySqliteHelper(Context context){
super(context,Constant.DATABASE_NAME,null,Constant.DATABASE_VERSION);
}
//数据库创建时回调
@Override
public void onCreate(SQLiteDatabase db){
Log.i("tag","--onCreate--");
//String sql="create table student(_id Integer primary key,name verchar(10),age Integer)";
String sql = "create table "+Constant.TABLE_NAME+"("+Constant._ID+" Integer primary key,"+Constant.NAME+" varchar(10),"+Constant.AGE+" Integer)";
db.execSQL(sql);//执行数据库语句
}
//数据库更新
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion){
Log.i("tag","--onUpgrade--");
}
//数据库打开
@Override
public void onOpen(SQLiteDatabase db){
super.onOpen(db);
Log.i("tag","--onOpen--");
}
}
//创建库表
public class Contant{
public static final String DATABASE_NAME=“info.db”;//数据库名称
public static final int DATABASE_VERSION=1;//数据库的版本号
public static final String TABLE_NAME="student";//表名
//用这里表示
public static final String _ID="_id";
public static final String _NAME="name";
public static final String AGE="age";
public class DbManger{
private static MySqliteHelper helper;
public static MySqliteHelper getIntance(Context context){
if(helper == null){
helper=new MySqliteHelper(content);
}
return hepler;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
}
}
public class DbManger{
private static MySqliteHelper helper;
public static MySqliteHelper getIntance(Context context){
if(helper == null){
helper=new MySqliteHelper(content);
}
return hepler;
}
public static void execSQL(SQLiteDatabase db,String sql){
if(db!=null){
if(sql!=null && !"".equals(sql)){
db.execSQL(sql);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
case R.id.btn_update:
db=helper.getWritableDatabase();
String updateSql="update "+Constant.TABLE_NAME"+" set "+Contant.NAME+"='vic2' where "+Contant._ID+"=1";
DbManger.execSQL(db,updateSql);
db.close();
break;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
case R.id.btn_update:
db=helper.getWritableDatabase();
String updateSql="update "+Constant.TABLE_NAME"+" set "+Contant.NAME+"='vic2' where "+Contant._ID+"=1";
DbManger.execSQL(db,updateSql);
db.close();
break;
case R.id.btn_delete:
db=helper.getWritableDatabase();
String delSql="delete from "+Constant.TABLE_NAME+" where "+Constant._ID+"=2";
DbManger.execSQL(db,delSql);
db.close();
break;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_insertApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void onClick(View view){
switch(view.getId()){
case R.id.btn_insertApi:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Constant._ID,3);
values.put(Constant.NAME,"vic");
values.put(Constant.AGE,17);
long result=db.insert(Constant.TABLE_NAME,null,values);
if(result>0){
Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
db.close();
break;
}
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
case R.id.btn_update:
db=helper.getWritableDatabase();
String updateSql="update "+Constant.TABLE_NAME"+" set "+Contant.NAME+"='vic2' where "+Contant._ID+"=1";
DbManger.execSQL(db,updateSql);
db.close();
break;
case R.id.btn_delete:
db=helper.getWritableDatabase();
String delSql="delete from "+Constant.TABLE_NAME+" where "+Constant._ID+"=2";
DbManger.execSQL(db,delSql);
db.close();
break;
}
}
<Button
android:id="@+id/btn_insertApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入数据"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_updateApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新数据"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
public void onClick(View view){
switch(view.getId()){
case R.id.btn_insertApi:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Constant._ID,3);
values.put(Constant.NAME,"vic");
values.put(Constant.AGE,17);
long result=db.insert(Constant.TABLE_NAME,null,values);
if(result>0){
Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
db.close();
break;
case R.id.btn_updateApi:
//String table 修改的数据表的名称,ContentValues values,String whereClause 表示修改条件,String[] whereArgs
db=helper.getWritableDatabase();
//db.update(String table,ContentValues values,String whereClause,String[] whereArgs);
ContentValues cv=new ContentValues();
cv.put(Contant.NAME,"vic3");
int count=db.update(Constant.TABLE_NAME,cv,Contant.TABLE_NAME,cv,Constant._ID+"=?",new String[]{"3"});
if(count>0){
Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
db.close();
break;
}
}
<Button
android:id="@+id/btn_updateApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新数据"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
public void onClick(View view){
switch(view.getId()){
case R.id.btn_insertApi:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Constant._ID,3);
values.put(Constant.NAME,"vic");
values.put(Constant.AGE,17);
long result=db.insert(Constant.TABLE_NAME,null,values);
if(result>0){
Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
db.close();
break;
case R.id.btn_updateApi:
//String table 修改的数据表的名称,ContentValues values,String whereClause 表示修改条件,String[] whereArgs
db=helper.getWritableDatabase();
//db.update(String table,ContentValues values,String whereClause,String[] whereArgs);
ContentValues cv=new ContentValues();
cv.put(Contant.NAME,"vic3");
int count=db.update(Constant.TABLE_NAME,cv,Contant.TABLE_NAME,cv,Constant._ID+"=?",new String[]{"3"});
if(count>0){
Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
db.close();
break;
case R.id.btn_deleteApi:
db=helper.getWritableDatabase();
//int count2=db.delete(String table,StringwhereClause,String[] whereArgs);
int count2=db.delete(Constant.TABLE_NAME,Constant._ID+"=?",new String[]{"1"});
if(count2>0){
Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
db.close();
break;
}
public int delete(String table,String whereClause,String[] whereArgs){
acquireReference();
try{
SQLiteStatement statement = new SQLiteStatement(this,"DELETE FROM "+table+(!TextUtils.isEmpty(whereClause) ? " WHERE "+whereClause : ""), whereArgs;
try{
return statement.executeUpdateDelete();
}finally{
statement.close();
}
}finally{
releaseReference();
}
这是一个有质量,有态度的公众号
喜欢本文的朋友们
欢迎长按下图关注订阅号
收看更多精彩内容