android自带例子apidemos

本文分享了多种Android应用UI设计及交互实现方法,包括ListView、动画效果、对话框、透明主题、定时器等,帮助开发者提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 android:id="@+id/happy"

android:title="Happy"

android:icon="@drawable/stat_happy" />

<item android:id="@+id/neutral"

android:title="Neutral"

android:icon="@drawable/stat_neutral" />

<item android:id="@+id/sad"

android:title="Sad"

android:icon="@drawable/stat_sad" />

</menu>

MORE: MenuInflateFromXml.Java

18 自定义的Toast
protected void showToast() {

// create the view

View view = inflateView(R.layout.incoming_message_panel);

// set the text in the view

TextView tv = (TextView)view.findViewById(R.id.message);

tv.setText("khtx. meet u for dinner. cul8r");

// show the toast

Toast toast = new Toast(this);

toast.setView(view);

toast.setDuration(Toast.LENGTH_LONG);

toast.show();

}

private View inflateView(int resource) {

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

return vi.inflate(resource, null);

}


19. Notification -- see IncomingMessage.java
20.ConditionVariable – block thread
NotifyingService.java :

private ConditionVariable mCondition;

@Override

public void onCreate() {

// Start up the thread running the service. Note that we create a

// separate thread because the service normally runs in the process's

// main thread, which we don't want to block.

Thread notifyingThread = new Thread(null, mTask, "NotifyingService");

mCondition = new ConditionVariable(false);

notifyingThread.start();

}

private Runnable mTask = new Runnable() {

public void run() {

for (int i = 0; i < 4; ++i) {

showNotification(R.drawable.stat_happy,

R.string.status_bar_notifications_happy_message);

if (mCondition.block(5 * 1000))

break;

showNotification(R.drawable.stat_neutral,

R.string.status_bar_notifications_ok_message);

if (mCondition.block(5 * 1000))

break;

showNotification(R.drawable.stat_sad,

R.string.status_bar_notifications_sad_message);

if (mCondition.block(5 * 1000))

break;

}

// Done with our work... stop the service!

NotifyingService.this.stopSelf();

}

};

About mCondition.block(long timeout): Block the current thread until the condition is opened or until timeout milliseconds have passed.

If the condition is already opened, return immediately.

Parameters

timeout
the minimum time to wait in milliseconds.

Returns

true if the condition was opened, false if the call returns because of the timeout.

21. prreferences from XML – 很多种,汗!
Public class PreferencesFromXml extends PreferenceActivity{

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Load the preferences from an XML resource

addPreferencesFromResource(R.xml.preferences);

}

}

See: res/xml/preferences.xml

22 views
22.1 auto complete
A. Screen top/bottom

setContentView(R.layout.autocomplete_1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_dropdown_item_1line, COUNTRIES);

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);

textView.setAdapter(adapter);

22.2. 不同的button

<!-- Regular sized buttons -->

<Button android:id="@+id/button_normal"

android:text="@string/buttons_1_normal"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<!-- Small buttons -->

<Button android:id="@+id/button_small"

style="?android:attr/buttonStyleSmall"

android:text="@string/buttons_1_small"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<ToggleButton android:id="@+id/button_toggle"

android:text="@string/buttons_1_toggle"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

22.3. chronometer --- Class that implements a simple timer.


22.4. write a custom subclass of View
LabelView.java

22.5.Expandable list – ExpandableList1/2/3.java

22.6 Layouts

22.7 Lists
22.8 progress bar
A. // Request the progress bar to be shown in the title

// a.

requestWindowFeature(Window.FEATURE_PROGRESS);

setProgressBarVisibility(true);

// b.

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setProgressBarIndeterminateVisibility(mToggleIndeterminate);

B. 不同大小的progress bar

style="?android:attr/progressBarStyleLarge"

style="?android:attr/progressBarStyleSmall"

style="?android:attr/progressBarStyleSmallTitle"

22.9 Rating bar


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/lbinuse/archive/2010/05/20/5611731.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值