提示框,菜单,数据存储,组件篇
Toast
Toast.makeText(context, text, 时间).show();
setDuration();//设置时间
setGravity();//位置
获取:
添加toast.getView();
imageView添加到toast中,addView();
LayoutInflater inflater = LayoutInflater.from(this);
View toast_view=inflater.inflate(R.id.,null);
Toast.
Toast.setView(toast_view);
AlertDialog
AlertDialog.Builder
setTitle();
setIcon();
setView();
setItems();
setMultiChoiceItems(); 复选
setSingleChoiceItems(); 单选
setNeutralButton();
setPositiveButton();
setNegativeButton();
AlertDialog dialog = builder.create(); .show();
消失
dialog.dismiss();
Notification通知栏,如接收短信
属性
Notification
setTicker();//手机状态栏的提示
setWhen();//设置时间
setContentTitle();
setContentText();
setContentIntent();
Notification notification = builder.build();
OptionsMenu
onCreateOptionsMenu();
动态:menu.add();
监听:onOptionsItemSelected();
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
监听:onOptionsItemSelected
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id..:
...
}
}
ContextMenu上下文菜单
public boolean onCreateItemSelected(MenuItem item){
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuIfo);
}
SubMenu 子菜单
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
<menu ...>
<item
android:showAsAction="never"
android:title="文件">
<menu>
<item
android:id="@+id/.."
android:showAsAction="never"
android:title="打开"/>
</menu>
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);
SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putXXX();
editor.commit();
用户名和密码的存储
sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
Editor editor = sharedPreferences.editor();
登录
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
DBOpenHelper helper = new DBOpenHelper(MainActivity.this, "user.db");
SQLiteDatabase db = helper.getWritableDatabase();
Cursor c = db.rawQuery("select * from user", null);
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){
}
File file = this.getFilesDir();
File file = this.getCacheDir();//重要文件就不要放在这里
MODE_PRIVATE
默认操作
MODE_APPEND
文件是否存在
public void WriteFiles(String content){
try{
FileOutputStream fileOutputStream = openFileOutput("super.txt", MODE_PRIVATE);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
}catch(){
}catch(){
}
}
ContentProvider
<provider
android:name=""
android:authorities=""/>
public class MyContentProvider extends ContentProvider{
public int delete(Uri uri,String selection, String[] selectionArgs){
}
}
Uri 通用资源标志符
Broadcast广播
BroadcastReceiver 广播接收器
使用:需要Intent对象,sendBroadcast(),sendOrderBroadcast(),sendStickyBroadcast()。
Service
注册:
<service android:name=""/>
public void onClick(View v){
switch(v.getId()){
case R.id.:
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
break;
}
}
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
public void onCreate(){
}
public IBinder onBind(Intent intent){
}
public void unbindService(ServiceConnection conn){
}
ServiceConnection conn = new ServiceConnection(){
public void onServiceDisconnected(ComponentName name){
}
public void onServiceConnected(ComponentName name, IBinder binder){
}
}
系统服务
MountService,ClipboardService,getSystemService().
public Object getSystemService(String name){
}
POWER_SERVICE,PowerManger电源服务
ALARM_SERVICE,AlarmManager闹钟服务
LayoutInflater inflater = MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout. , null);
setContentView(view);
getSystemService
GestureDetector
MotionEvent,GestureDetector,onGestureListener.
GestureOverlayView
Android:eventsInterceptionEnabled
Android:fadeDuration
Android:fadeEnabled
Android:gestureColor
<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 知识点
完结!
原文地址:https://www.cnblogs.com/dashucoding/p/9281733.html