提示框,菜单,数据存储,组件篇
Toast
Toast.makeText(context, text, 时间).show();
setDuration();//设置时间 setGravity();//位置
获取:
添加toast.getView(); imageView添加到toast中,addView();
代码语言:javascript复制LayoutInflater inflater = LayoutInflater.from(this);
View toast_view=inflater.inflate(R.id.,null);
Toast.
Toast.setView(toast_view);
AlertDialog
AlertDialog.Builder
代码语言:javascript复制setTitle();
setIcon();
setView();
setItems();
setMultiChoiceItems(); 复选
setSingleChoiceItems(); 单选
setNeutralButton();
setPositiveButton();
setNegativeButton();
代码语言:javascript复制AlertDialog dialog = builder.create(); .show();
消失
dialog.dismiss();
Notification通知栏,如接收短信
代码语言:javascript复制属性
Notification
setTicker();//手机状态栏的提示
setWhen();//设置时间
setContentTitle();
setContentText();
setContentIntent();
Notification notification = builder.build();
OptionsMenu
代码语言:javascript复制onCreateOptionsMenu();
动态:menu.add();
监听:onOptionsItemSelected();
代码语言:javascript复制public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
监听:onOptionsItemSelected
代码语言:javascript复制public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id..:
...
}
}
ContextMenu上下文菜单
代码语言:javascript复制public boolean onCreateItemSelected(MenuItem item){
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuIfo);
}
SubMenu 子菜单
代码语言:javascript复制protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
//return super.onOptionsItemSelected(item);
}
xml
代码语言:javascript复制<menu ...>
<item
android:showAsAction="never"
android:title="文件">
<menu>
<item
android:id="@ id/.."
android:showAsAction="never"
android:title="打开"/>
</menu>
代码语言:javascript复制MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
数据存储方式
四种:
- SharedPreferences
- SQLite
- Content Provider
- File
SharedPreferences是一种比较轻型的数据存储,基于xml的键值对存储,存储一些简单的信息。
SharedPreferences只能获取数据不能存储和修改,但能通过Editor实现存储修改。
步骤:
- 获取SharedPreferences对象
- SharedPreferences.Editor
- Editor的putXXX的方法
- Editor.commit()
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
代码语言:javascript复制SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putXXX();
editor.commit();
用户名和密码的存储
代码语言:javascript复制sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
Editor editor = sharedPreferences.editor();
代码语言:javascript复制登录
String name = etName.getText().toString().trim();
String pass = etPass.getText().toString().trim();
SQLite的形式,以单个文件的存储,存储2T内存,以B-Tree形式。
SQLite的数据类型,null,integer,real,text,blob。
- SQLiteDatabase
- SQLiteOpenHelper
SQLiteDatabase db = openOrCreateDatabase("table.db", MODE_PRIVATE, null);
SQLiteOpenHelper
代码语言:javascript复制DBOpenHelper helper = new DBOpenHelper(MainActivity.this, "user.db");
SQLiteDatabase db = helper.getWritableDatabase();
Cursor c = db.rawQuery("select * from user", null);
代码语言:javascript复制public DBOpenHelper(Context context, String name, CursorFactory factory, int version){
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db){
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
代码语言:javascript复制File file = this.getFilesDir();
File file = this.getCacheDir();//重要文件就不要放在这里
MODE_PRIVATE
默认操作
MODE_APPEND
文件是否存在
代码语言:javascript复制public void WriteFiles(String content){
try{
FileOutputStream fileOutputStream = openFileOutput("super.txt", MODE_PRIVATE);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
}catch(){
}catch(){
}
}
ContentProvider
代码语言:javascript复制<provider
android:name=""
android:authorities=""/>
代码语言:javascript复制public class MyContentProvider extends ContentProvider{
public int delete(Uri uri,String selection, String[] selectionArgs){
}
}
Uri 通用资源标志符
Broadcast广播
BroadcastReceiver 广播接收器
使用:需要Intent对象,sendBroadcast(),sendOrderBroadcast(),sendStickyBroadcast()。
Service
注册:
代码语言:javascript复制<service android:name=""/>
代码语言:javascript复制public void onClick(View v){
switch(v.getId()){
case R.id.:
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
break;
}
}
代码语言:javascript复制public class MyService extends Service{
public void onCreate(){
super.onCreate();
}
public int onStartCommand(Intent intent,int flags,int startId){
return super.onStartCommand(intent,flags,startId);
}
public void onDestroy(){
super.onDestroy();
}
public IBinder onBind(Intent intent){
return null;
}
}
Bind
代码语言:javascript复制public void onCreate(){
}
public IBinder onBind(Intent intent){
}
public void unbindService(ServiceConnection conn){
}
代码语言:javascript复制ServiceConnection conn = new ServiceConnection(){
public void onServiceDisconnected(ComponentName name){
}
public void onServiceConnected(ComponentName name, IBinder binder){
}
}
系统服务
MountService,ClipboardService,getSystemService().
代码语言:javascript复制public Object getSystemService(String name){
}
POWER_SERVICE,PowerManger电源服务 ALARM_SERVICE,AlarmManager闹钟服务
代码语言:javascript复制LayoutInflater inflater = MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout. , null);
setContentView(view);
getSystemService
GestureDetector
代码语言:javascript复制MotionEvent,GestureDetector,onGestureListener.
GestureOverlayView
代码语言:javascript复制Android:eventsInterceptionEnabled
Android:fadeDuration
Android:fadeEnabled
Android:gestureColor
代码语言:javascript复制<android.gesture.GestureOverlayView
android:id=""
android:layout_width=""
android:layout_height="">
<ImageView
android:id=""
android:layout_width=""
android:layout_height=""
</android.gesture.GestureOverlayView>
定位:分享 Android&Java 知识点 完结!