2011年6月21日星期二

子菜單(SubMenu)

使用子菜單(SubMenu)的工作示例:

子菜單(SubMenu)

package com.AndroidMenu;

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class AndroidMenuActivity extends Activity {

final static int OPTION_ID_1 = 1;
final static int OPTION_ID_2 = 2;
final static int OPTION_ID_3 = 3;
final static int OPTION_ID_4 = 4;
final static int OPTION_ID_5 = 5;

final static int OPTION_ID_6 = 6;
final static int OPTION_ID_6_1 = 61;
final static int OPTION_ID_6_2 = 62;
final static int OPTION_ID_6_3 = 63;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);

//menu.add(Group, item id, order, title);
menu.add(0, OPTION_ID_1, 0, "Option 1");
menu.add(0, OPTION_ID_2, 1, "Option 2");
menu.add(0, OPTION_ID_3, 2, "Option 3");
menu.add(0, OPTION_ID_4, 3, "Option 4");
menu.add(0, OPTION_ID_5, 4, "Option 5");

SubMenu subMenu6 = menu.addSubMenu(0, OPTION_ID_5, 5, "SubMenu");
subMenu6.add(0, OPTION_ID_6_1, 0, "SubMenu6.1");
subMenu6.add(0, OPTION_ID_6_2, 0, "SubMenu6.2");
subMenu6.add(0, OPTION_ID_6_3, 0, "SubMenu6.3");

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

switch(item.getItemId()){
case OPTION_ID_1:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_2:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_3:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_4:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_5:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;

}

return true;
}


}



相關文章:
- 選項菜單(OptionsMenu)

2011年6月20日星期一

選項菜單(OptionsMenu)

當用戶按 MENU 硬件按鈕, Android 系統會顯示選項菜單(OptionsMenu)

選項菜單(OptionsMenu)

應用程序可以重寫 onCreateOptionsMenu(Menu menu) 方法創建選項菜單(OptionsMenu), 並重寫 onOptionsItemSelected(MenuItem item) 方法處理事件.

範例:
package com.AndroidMenu;

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidMenuActivity extends Activity {

final static int OPTION_ID_1 = 1;
final static int OPTION_ID_2 = 2;
final static int OPTION_ID_3 = 3;
final static int OPTION_ID_4 = 4;
final static int OPTION_ID_5 = 5;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);

//menu.add(Group, item id, order, title);
menu.add(0, OPTION_ID_1, 0, "Option 1");
menu.add(0, OPTION_ID_2, 1, "Option 2");
menu.add(0, OPTION_ID_3, 2, "Option 3");
menu.add(0, OPTION_ID_4, 3, "Option 4");
menu.add(0, OPTION_ID_5, 4, "Option 5");

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

switch(item.getItemId()){
case OPTION_ID_1:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_2:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_3:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_4:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;
case OPTION_ID_5:
Toast.makeText(AndroidMenuActivity.this,
item.getTitle(),
Toast.LENGTH_LONG).show();
break;

}

return true;
}
}



相關文章:
- 子菜單(SubMenu)

如何設置應用程序的背景為透明

可以修改清單文件, AndroidManifest.xml, 在 <activity> 裡面添加代碼 android:theme="@android:style/Theme.Translucent".

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.AndroidTranslucent"
    android:versionCode="1"
    android:versionName="1.0">
  <uses-sdk android:minSdkVersion="4" />
 
  <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".AndroidTranslucent"
                android:label="@string/app_name"
                android:theme="@android:style/Theme.Translucent">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
 
  </application>
</manifest>


2011年6月9日星期四

谷歌推出 Android 的手勢搜索應用程序接口(Gesture Search API)


谷歌實驗室(Google Labs)發布手勢搜索應用程序接口(Gesture Search API), 它可以讓開發人員把手勢搜索整合到應用程序,使得用戶可以手寫輸入文字和搜索。

谷歌官方Code博客(The Official Google Code Blog)有一篇文章, Add Gesture Search to your Android apps,展示我們如何能夠嵌入手勢搜索(1.4.0或更新版本)到一個 Android應用程序。

2011年6月8日星期三

使用意圖(Intent)啟動 Android 內建谷歌地圖的街景(StreetView)

啟動 Android 內建谷歌地圖的街景(StreetView), 可以使用:
"google.streetview:cbll=<lat>,<lon>

使用意圖(Intent)啟動 Android 內建谷歌地圖的街景(StreetView)
使用意圖(Intent)啟動 Android 內建谷歌地圖的街景(StreetView)

package com.AndroidIntentGoogleMaps;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AndroidIntentGoogleMaps extends Activity {

EditText inputLatitude, inputLongitude;
Button buttonStartMaps;

final static float DEFAULT_Latitude = (float) 40.75773;
final static float DEFAULT_Longitude = (float) -73.985708;

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

inputLatitude = (EditText)findViewById(R.id.Latitude);
inputLongitude = (EditText)findViewById(R.id.Longitude);
buttonStartMaps = (Button)findViewById(R.id.startmaps);

inputLatitude.setText(String.valueOf(DEFAULT_Latitude));
inputLongitude.setText(String.valueOf(DEFAULT_Longitude));

buttonStartMaps.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String lat = inputLatitude.getText().toString();
String lon = inputLongitude.getText().toString();

String strUri = "google.streetview:cbll="+lat+","+lon;

Toast.makeText(AndroidIntentGoogleMaps.this, strUri, Toast.LENGTH_LONG).show();

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
startActivity(intent);
}});

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude" />
<EditText
android:id="@+id/Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude" />
<EditText
android:id="@+id/Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom" />
<Button
android:id="@+id/startmaps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Street View" />
</LinearLayout>



相關文章:
- 使用意圖(Intent)啟動 Android 內建谷歌地圖

2011年6月7日星期二

使用意圖(Intent)啟動 Android 內建谷歌地圖

通過意圖(Intent), 應用程序可啟動 Android 內建谷歌地圖; 指定地點及縮放程度. 下面是一個例子.

使用意圖(Intent)啟動 Android 內建谷歌地圖
使用意圖(Intent)啟動 Android 內建谷歌地圖

package com.AndroidIntentGoogleMaps;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

public class AndroidIntentGoogleMaps extends Activity {

EditText inputLatitude, inputLongitude;
SeekBar seekbarZoom;
Button buttonStartMaps;

final static float DEFAULT_Latitude = (float) 40.75773;
final static float DEFAULT_Longitude = (float) -73.985708;

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

inputLatitude = (EditText)findViewById(R.id.Latitude);
inputLongitude = (EditText)findViewById(R.id.Longitude);
seekbarZoom = (SeekBar)findViewById(R.id.zoom);
buttonStartMaps = (Button)findViewById(R.id.startmaps);

inputLatitude.setText(String.valueOf(DEFAULT_Latitude));
inputLongitude.setText(String.valueOf(DEFAULT_Longitude));

buttonStartMaps.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String lat = inputLatitude.getText().toString();
String lon = inputLongitude.getText().toString();
String zoom = String.valueOf(seekbarZoom.getProgress()+1);
String strUri = "geo:"+lat+","+lon+"?z="+zoom;

Toast.makeText(AndroidIntentGoogleMaps.this, strUri, Toast.LENGTH_LONG).show();

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
startActivity(intent);
}});

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude" />
<EditText
android:id="@+id/Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude" />
<EditText
android:id="@+id/Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom" />
<SeekBar
android:id="@+id/zoom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="22"
android:padding="10dp" />
<Button
android:id="@+id/startmaps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Google Maps" />
</LinearLayout>



相關文章:
- 使用意圖(Intent)啟動 Android 內建谷歌地圖的街景(StreetView)

Android 開發工具 ADT 11 已供下載,改進編輯功能

ADT 11 重點改進編輯器。
  • 首先,它提供了一些新的視覺重構操作,如"Extract Include'和"Extract Style". 這有助於自動提取片段重複佈局和樣式屬性為可重複使用的佈局,樣式和主題。
  • 第二,可視化佈局編輯器現在支持的片段,調色板配置,更好地支持自定義視圖。
  • 最後,已經改良XML編輯,更多文件類型的代碼完成(code completion)和增強許多"轉至聲明"(go to declaration)。


來源:
- Android開發者博客: New Editing Features in Eclipse plug-in for Android