2009年12月31日星期四

上下文菜單(ContextMenu)

Android的上下文菜單(ContextMenu)類似選項菜單(OptionsMenu), 上下文菜單可以註冊到任何視圖對象(View Object), 最常用在ListView的選項上. 當一個視圖註冊了一個上下文菜單(使用registerForContextMenu(View view)), 在該視圖對象上“長按”(按住約 2秒), 將顯示一個浮動的上下文菜單,提供有關該項目的相關功能.

ContextMenu

上下文菜單項不支持圖標或快捷鍵.

要創建一個上下文菜單, 必須重寫Activity的上下文菜單回調方法: onCreateContextMenu() 和 onContextItemSelected(). 在onCreateContextMenu()回調方法中, 可以使用add()方法添加單項, 或擴展一個XML定義的菜單資源(menu resource). 然後使用registerForContextMenu()將ContextMenu註冊到視圖上.

在這個例子中, 我們將註冊一個上下文菜單在整個屏幕上, 因此, 首先修改main.xml為整個LinearLayout添加一個id.
<?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"
android:id="@+id/wholeview"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


然後修改主程序重寫onCreateContextMenu() 和 onContextItemSelected(), 並在onCreate()方法中把上下文菜單註冊到LinearLayout上.
package com.AndroidContextMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class AndroidContextMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout wholeWiew = (LinearLayout)findViewById(R.id.wholeview);
registerForContextMenu(wholeWiew);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case 0:
Toast.makeText(this, "Option 0", Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this, "Option 1", Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, "Option 2", Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}


}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 0, 0, "Option 0");
menu.add(0, 1, 0, "Option 1");
menu.add(0, 2, 0, "Option 2");
}
}




AlertDialog.Builder, 可能是創建對話框最簡單的方法.

AlertDialog.Builder是一個公共靜態類(public static class), 即是可以直接調用創建一個警告對話框(AlertDialog). 它可能是創建對話框最簡單的方法.

AlertDialog


new AlertDialog.Builder(this)
.setTitle("Title").setMessage("Message")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();




使用Menu.add(int groupId, int itemId, int order, CharSequence title)設定Options Menu

除了使用XML定義Options Menu外, OptionsMenu亦可以程式碼實現, 使用Menu.add(int groupId, int itemId, int order, CharSequence title)設定Options Menu.

Options Menu

修改主程序, 重寫onCreateOptionsMenu(Menu)和onOptionsItemSelected(MenuItem):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 0, 0, "Option 0");
menu.add(0, 1, 1, "Option 1");
menu.add(0, 2, 2, "Option 2");
menu.add(0, 3, 3, "Option 3");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (0):
break;
case (1):
break;
case (2):
break;
case (3):
break;
}
return true;
}





使用XML定義Options Menu(選項菜單)

Options Menu

首先創建一個文件夾, /res/menu

並創建一個新/res/menu/的menu.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/opt1"
android:title="Option 1" />
<item android:id="@+id/opt2"
android:title="Option 3" />
<item android:id="@+id/opt3"
android:title="Option 3" />
</menu>


修改主程序, 重寫onCreateOptionsMenu(Menu)和onOptionsItemSelected(MenuItem):
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (R.id.opt1):
break;
case (R.id.opt2):
break;
case (R.id.opt3):
break;
}
return true;
}



除了使用XML外, OptionsMenu亦可以程式碼實現, 使用Menu.add(int groupId, int itemId, int order, CharSequence title)設定Options Menu.



使用ImageButton.setImageResource加載圖像

上一篇文章闡釋了如何使用XML定義ImageButton, 亦可以使用程式碼實現類似的效果.


預先準備三個按鈕的圖像, 分別顯示normal, focused 和 pressed 三種狀態, 並且儲存到/res/drawable-mdpi/文件夾中.

normalfocusedpressed

修改佈局文件(/res/layout/main.xml)
<?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"
/>
<ImageButton
android:id="@+id/imagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/normal"
/>
</LinearLayout>


在主程序重寫setOnFocusChangeListener()和setOnClickListener(), 使用ImageButton.setImageResource加載圖像.
package com.AndroidImageButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class AndroidImageButton extends Activity {

ImageButton imageButton;

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

imageButton = (ImageButton)findViewById(R.id.imagebutton);

imageButton.setOnClickListener(imageButtonOnClickListener);
imageButton.setOnFocusChangeListener(imageButtonOnFocusChangeListener);
}

private ImageButton.OnClickListener imageButtonOnClickListener
= new ImageButton.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
imageButton.setImageResource(R.drawable.pressed);
}

};

private ImageButton.OnFocusChangeListener imageButtonOnFocusChangeListener
= new ImageButton.OnFocusChangeListener(){

@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus==true)
{
imageButton.setImageResource(R.drawable.focused);
}
else
{
imageButton.setImageResource(R.drawable.normal);
}
}

};
}

2009年12月30日星期三

如何使用XML定義ImageButton?

ImageButton這個名字意味著它是一個有圖像的按鈕, 而且可以在不同狀態(normal, focused and pressed)有不同圖像的按鈕.

Normal:
normal

Focused:
focused

Pressed:
pressed

預先準備三個按鈕的圖像, 分別顯示normal, focused 和 pressed 三種狀態, 並且儲存到/res/drawable-mdpi/文件夾中.

normalfocusedpressed

在/res/drawable-mdpi/文件夾中創建一個selector.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/focused" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item
android:drawable="@drawable/normal" />
</selector>



修改佈局文件(/res/layout/main.xml)
<?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"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selector"
/>
</LinearLayout>


除了使用XML, 亦可使用ImageButton.setImageResource加載圖像.



RelativeLayout (相對佈局)

RelativeLayout可讓子視圖根據他的父視圖或其他視圖指定本身的相對位置. 因此, 可以根據各種條件對齊兩個元件; 例如: 右邊, 以下, 中間, 中間偏左...等等.

元件的表現是根據提出的次序, 所以如果使用XML來指定這個佈局, 被用作定位的元件必須在被參考前列出.

例子:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="OK" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:text="http://androidbiancheng.blogspot.com/" />
</RelativeLayout>



相關文章:
- 如何通過相對佈局(RelativeLayout)把多個圖像重疊放置於中央