Android Development Engineer Anthology-Cue box, menu, data store, component chapter

Source: Internet
Author: User

Cue box, menu, data store, component toast

Toast.maketext (context, text, time). Show ();

Setduration ();//Set time
Setgravity ();//Location

Get:

Add Toast.getview ();
ImageView added to 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 bar, such as receiving SMS

属性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;}

Listening: onoptionsitemselected

public boolean onOptionsItemSelected(MenuItem item){    switch(item.getItemId()){        case R.id..:        ...    }}
ContextMenu context Menu
public boolean onCreateItemSelected(MenuItem item){}public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){    super.onCreateContextMenu(menu, v, menuIfo);}
Submenu Sub-Menu
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);
How data is stored

Four kinds:

    1. Sharedpreferences
    2. Sqlite
    3. Content Provider
    4. File

Sharedpreferences is a relatively lightweight data store that stores some simple information based on XML key-value pairs.

Sharedpreferences can only get data can not be stored and modified, but can be implemented through the editor storage modifications.

Steps:

    1. Get Sharedpreferences Object
    2. Sharedpreferences.editor
    3. The Putxxx method of editor
    4. Editor.commit ()
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences", MODE_PRIVATE);Editor editor = sharedPreferences.edit();editor.putXXX();editor.commit();

Storage of user names and passwords

sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);Editor editor = sharedPreferences.editor();
登录String name = etName.getText().toString().trim();String pass = etPass.getText().toString().trim();

SQLite forms, stored in a single file, stores 2T of memory in b-tree form.

The data type of SQLite, Null,integer,real,text,blob.

    1. Sqlitedatabase
    2. 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

Default action

Mode_append

Whether the file exists

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 Common Resource Identifier

Broadcast broadcast

Broadcastreceiver Broadcast Receivers

Use: Requires Intent object, Sendbroadcast (), Sendorderbroadcast (), Sendstickybroadcast ().

Service

Registered:

<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){    }}

System Services

Mountservice,clipboardservice,getsystemservice ().

public Object getSystemService(String name){}

Power_service,powermanger Power Supply Service
Alarm_service,alarmmanager Alarm Clock Service

LayoutInflater inflater = MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);View view = inflater.inflate(R.layout. , null);setContentView(view);

Getsystemservice

Gesturedetector

MotionEvent,GestureDetector,onGestureListener.

Gestureoverlayview

Android:eventsInterceptionEnabledAndroid:fadeDurationAndroid:fadeEnabledAndroid:gestureColor
<android.gesture.GestureOverlayView  android:id="" android:layout_width="" android:layout_height=""> <ImageView  android:id=""  android:layout_width=""  android:layout_height=""</android.gesture.GestureOverlayView>  

Location: Share Android&java knowledge points
End!

Android Development Engineer Anthology-Cue box, menu, data store, component chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.