提示框,菜單,數據存儲,組件篇 Toast Toast.makeText(context, text, 時間).show(); setDuration();//設置時間 setGravity();//位置 獲取: 添加toast.getView(); imageView添加到toast中,addVi ...
提示框,菜單,數據存儲,組件篇
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 知識點
完結!