此範例為一個簡單菜單範例,使用者可任意選取所要的項目,
使用者每次選取/取消餐點,就即時記錄已選取的項目,
MainActivity.java
package tw.com.flag.ch05_foodmenuevent;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 所有核取方塊 ID 的陣列
int[] chk_id={R.id.chk1,R.id.chk2,R.id.chk3,R.id.chk4,
R.id.chk5,R.id.chk6,R.id.chk7,R.id.chk8};
for(int id:chk_id){ // 用迴圈替所有核取方塊加上監聽物件
CheckBox chk=(CheckBox) findViewById(id);
chk.setOnCheckedChangeListener(this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void takeOrder(View v) {
String msg=""; // 儲存訊息的字串
for(CompoundButton i:selected) // 以迴圈逐一將換行字元及
msg+="\n"+i.getText(); // 選項文字附加到 msg 字串後面
if(msg.length()>0) // 有點餐
msg ="你點購的餐點是:"+msg;
else
msg ="請點餐!";
// 在文字方塊中顯示點購的項目
((TextView) findViewById(R.id.showOrder)).setText(msg);
}
// 用來儲存已選取項目的 ID 集合物件
ArrayList<CompoundButton> selected=new ArrayList<CompoundButton>();
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked==true) // 若項目被選取
selected.add(buttonView); // 加到集合之中
else // 若項目被取消
selected.remove(buttonView); // 自集合中移除
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<CheckBox
android:id="@+id/chk1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hambuger"
android:textSize="30sp" />
<CheckBox
android:id="@+id/chk2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fries"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<CheckBox
android:id="@+id/chk3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="coke"
android:textSize="30sp" />
<CheckBox
android:id="@+id/chk4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="soup"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<CheckBox
android:id="@+id/chk5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chicken"
android:textSize="30sp" />
<CheckBox
android:id="@+id/chk6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="salad"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<CheckBox
android:id="@+id/chk7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cafe"
android:textSize="30sp" />
<CheckBox
android:id="@+id/chk8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple pie"
android:textSize="30sp" />
</LinearLayout>
<Button
android:id="@+id/order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takeOrder"
android:text="order"
android:textSize="30sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
</LinearLayout>
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/showOrder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:onClick="takeOrder"
android:textSize="30dp" />
</ScrollView>
</LinearLayout>
文章標籤
全站熱搜
留言列表