還是接上一篇,這次妖精們說了,不要用RadioButton控制項了,不好看,還佔地方,抓一個人,要佔四個地方,那我抓一個人只要佔一個地方就行了,於是用了Spinner控制項,還是一次抓一個
main.xml如下
view plain
</pre><pre class="html" name="code"><?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">
<Spinner android:layout_height="wrap_content" android:id="@+id/spinner"
android:layout_width="wrap_content"></Spinner>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="@string/hello"
android:id="@+id/text"></TextView>
</LinearLayout>
Activity的java代碼如下:
view plain
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class ButtonDemoActivity extends Activity implements OnItemSelectedListener
{
private TextView text = null;
private Spinner spinner;
private String[] item = { "唐僧", "孫悟空 ", "豬八戒", "沙和尚" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 通過ID尋找到main.xml中的TextView控制項
text = (TextView) findViewById(R.id.text);
// 通過ID尋找到main.xml中的Spinner控制項
spinner = (Spinner) findViewById(R.id.spinner);
//設定一個Array適配器,將數組資料放入適配器中
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, item);
//設定下拉式清單的樣式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//對Spinner進行適配
spinner.setAdapter(adapter);
//Spinner中事件選擇的監聽
spinner.setOnItemSelectedListener(this);
}
private void updateText(String string)
{
// 將文本資訊設定給TextView控制項顯示出來
text.setText(string);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
String str = "這次妖精把" + item[position] + "抓住了!";
updateText(str);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
}
你要說妖精們的事還真多,一會這樣,一會那樣的,用我們現在的話來說就叫"眾口難調"啊,畢竟妖精多了去了,
摘自:kangkangz4的專欄