APP
1. Apidemos.java
// ListView的每个item是个map型数据,再从中取得intent!!
protected void onListItemClick(ListView l, View v, int position, long id) {
Map map = (Map) l.getItemAtPosition(position);
Intent intent = (Intent) map.get("intent");
startActivity(intent);
}
2. Animation.java
overridePendingTransition(R.anim.fade, R.anim.hold);
anim.fade.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
anim.hold.xml: 略
anim.zoom_enter.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale="2.0" android:toXScale="1.0"
android:fromYScale="2.0" android:toYScale="1.0"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
3. CustomDialogActivity.java
4. CustomTitle.java
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.custom_title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
5. DialogActivity.Java
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);
Activity 以 Dialog 形式存在:
只要在AndroidManifest.xml中设置该Activity属性为:android:theme="@android:style/Theme.Dialog"
6. 要想 输入进EditText的字符能自动保存,使用
mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
7. Reorder Activity
Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
8. 使Activity的界面 (半)透明
2种情况:
a. 在AndroidManifest.xml中设置该Activity属性为:
android:theme="@style/Theme.Translucent"
b. Have the system blur any windows behind this one。
在AndroidManifest.xml中设置该Activity属性为:
android:theme="@style/ Theme.Transparent"
9. 使Activity半透明,并在下面显示wallpaper
在AndroidManifest.xml中设置该Activity属性为:
android:theme="@style/ Theme.Wallpaper"
10. Alerm
A. one shot alerm
例子:当间隔时间(5秒)到达时,发一个broadcast给OneShotAlarm.class,使其给个toast:
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
// We want the alarm to go off 5 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
OneShotAlarm.java:
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show();
}
B. repeating alerm
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
long firstTime = SystemClock.elapsedRealtime();
firstTime += 15*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 15*1000, sender);
C. stop repeating alerm
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
D. Alerm service
11. xml里定义array (res/values/array.xml)
<string-array name="select_dialog_items">
<item>Command one</item>
<item>Command two</item>
<item>Command three</item>
<item>Command four</item>
</string-array>
使用:
new AlertDialog.Builder(AlertDialogSamples.this)
.setTitle(R.string.select_dialog)
.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
/* User clicked so do some stuff */
String[] items = getResources().getStringArray(R.array.select_dialog_items);
new AlertDialog.Builder(AlertDialogSamples.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();
}
}).create();
12. Handlr 给自身发message
mProgressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mProgress >= MAX_PROGRESS) {
mProgressDialog.dismiss();
} else {
mProgress++;
mProgressDialog.incrementProgressBy(1);
mProgressHandler.sendEmptyMessageDelayed(0, 1000);
}
}
};
More:sendEmptyMessage(int what),sendEmptyMessageAtTime(int what, long uptimeMillis),
sendMessage(Message msg),。。。。。
13. Signal choice list
new AlertDialog.Builder(AlertDialogSamples.this)
.setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked on a radio button do some stuff */
}
})
14. multiple choice list
new AlertDialog.Builder(AlertDialogSamples.this)
.setMultiChoiceItems(R.array.select_dialog_items3,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
/* User clicked on a check box do some stuff */
}
})
15. launching various intents
Get music:
Intent intent = new Intent(Intent . ACTION_GET_CONTENT); // not Intent.PICK
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Select music"));
16. launcher shortcuts
17. Menu inflate from XML
定义各种menu的xml资源:
private static final int sMenuExampleResources[] = {
R.menu.title_only, R.menu.title_icon, R.menu.submenu, R.menu.groups,
R.menu.checkable, R.menu.shortcuts, R.menu.order, R.menu.category_order,
R.menu.visible, R.menu.disabled
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Hold on to this
mMenu = menu;
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
return true;
}
XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item an