2012年4月19日星期四

Android 3.0 的彈出菜單(PopupMenu)

Android 3.0, API Level 11, 的彈出菜單(PopupMenu)在一個模態的彈出窗口(modal popup window)之上顯示菜單.

實例:

彈出菜單(PopupMenu)

創建/res/menu/popupmenu.xml文件, 定義彈出菜單(PopupMenu)的佈局.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/popupmenu">
<item android:id="@+id/menu1"
android:title="item 1"/>
<item android:id="@+id/menu2"
android:title="item 2"/>
<item android:id="@+id/menu3"
android:title="item 3"/>
</group>
</menu>


修改 main.xml 添加一個按鈕啟動彈出菜單.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/startpopupmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start PopupMenu"
/>

</LinearLayout>


主Java代碼.
package com.Android3PopupMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;

public class Android3PopupMenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonPopupMenu = (Button)findViewById(R.id.startpopupmenu);
buttonPopupMenu.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
PopupMenu popupMenu = new PopupMenu(Android3PopupMenuActivity.this, view);
popupMenu.getMenuInflater().inflate(R.menu.popupmenu, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

@Override
public boolean onMenuItemClick(MenuItem item) {
// Do something...
return true;
}

});

popupMenu.show();
}});
}
}

2012年4月11日星期三

彈出視窗(PopupWindow)

彈出視窗(PopupWindow)就像是一個浮動在主屏幕之上的視窗.

實例:

彈出視窗(PopupWindow)

修改main.xml, 添加一個按鈕用來打開彈出視窗(PopupWindow).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/openpopupwindow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open PopupWindow" />

</LinearLayout>


主要的Java代碼.
package com.PopupWindow;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupWindowActivity extends Activity {

Button buttonOpen;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonOpen = (Button)findViewById(R.id.openpopupwindow);
buttonOpen.setOnClickListener(buttonOpenOnClickListener);
}

Button.OnClickListener buttonOpenOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View view) {
openPopupWindow(view);
}};

private void openPopupWindow(View v){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button buttonClose = (Button)popupView.findViewById(R.id.closepopupwindow);
buttonClose.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});

popupWindow.showAsDropDown(buttonOpen);
popupWindow.setFocusable(true);
popupWindow.update();
}
}


創建/res/layout/popup.xml文件, 定義彈出視窗的佈局.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000055">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PopupWindow" />
<Button
android:id="@+id/closepopupwindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>


2012年4月7日星期六

使用managedQuery()獲取Android設備的視頻媒體內容

本程序演示如何使用managedQuery()方法, 從媒體存儲(MediaStore)獲取Android設備的視頻媒體內容. 把Uri設定為 MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 排序設定為 MediaStore.Video.Media.TITLE.

使用managedQuery()獲取Android設備的視頻媒體內容

package com.AndroidMediaList;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class AndroidMediaListActivity extends ListActivity {

//For Video
Uri targetUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String sortOrder = MediaStore.Video.Media.TITLE;
//For Audio
//Uri targetUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//String sortOrder = MediaStore.Audio.Media.TITLE;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] from = {MediaStore.MediaColumns.TITLE};
int[] to = {android.R.id.text1};

Cursor cursor = managedQuery(targetUri, null, null, null, sortOrder);

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
}
}

2012年4月5日星期四

獲取Android設備的音樂媒體內容

本程序演示如何使用managedQuery()方法從媒體存儲(MediaStore)獲取Android設備的音樂媒體內容. 把Uri設定為 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 排序設定為 MediaStore.Audio.Media.TITLE.

獲取Android設備的音樂媒體內容

package com.AndroidMediaList;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class AndroidMediaListActivity extends ListActivity {

Uri targetUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] from = {MediaStore.MediaColumns.TITLE};
int[] to = {android.R.id.text1};

Cursor cursor = managedQuery(targetUri, null, null, null, MediaStore.Audio.Media.TITLE);

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
}
}