項目中界面,有幾個數值不允許使用者輸入,隻能在下拉清單中選擇項目,一開始想過自定義dialog或者popupwindow,但是會額外增加很多代碼,考慮到現在的工程代碼量已經很多了,是以想到了使用google已經開發好的元件spinner元件,這是一個非常好用的系統下拉選項元件,具體的用法我就不多說了,有很多已經總結過了,大概的流程就是先設定spinner控件,如下:
Spinner<
android:id="@+id/touchprice"
android:layout_width="190dp"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:dropDownWidth="20dp"
android:prompt="@string/touch" /> <!--這裡設定首先顯示的是哪個數值 -->
之後可以在代碼上,設定預設下拉樣式和彈出模式,監聽選中的選項,這些都有很多例子可以參考,我的是這樣的:
// 第2個下拉清單,有效日期
spinner = (Spinner) view.findViewById(R.id.effectivedate);// 将可選内容與ArrayAdapter連接配接起來
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, m);</span>
</strong>
// //設定下拉清單的風格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 将adapter 添加到spinner中
spinner.setAdapter(adapter);
// 添加事件Spinner事件監聽
spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
// 設定預設值
spinner.setVisibility(View.VISIBLE);
}
// 使用數組形式操作
class SpinnerSelectedListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast tst = Toast.makeText(getView().getContext(),
"您點選了" + m[arg2], Toast.LENGTH_SHORT);
tst.show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
這樣就實作了最基本的系統下拉清單元件,如下:
是不是感覺特别的醜,特别是如下下面我的edittext是要用圓角來顯示的話,就顯得對比太大了,是以就到了關鍵的一步,不要使用系統自帶的清單項布局檔案,也就是上述的這段代碼:
// 将可選内容與ArrayAdapter連接配接起來
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, m);
// //設定下拉清單的風格
我們可以替換成我們想要表現的布局格式,下面是我要呈現的樣子,就是圓角,然後修改裡面預設字型的顔色,改為白色,并且居中顯示:
activity_tipsprice_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:gravity="center"
android:paddingTop="5dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="14dp"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"/>
這個隻是設定下拉的文字樣式,如果要圓角的話,必須在spinner元件設定android:background設定圓角的樣式,如下:
<Spinner
android:id="@+id/touchprice"
android:layout_width="190dp"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:background="@drawable/tipspriceshape"
android:dropDownWidth="20dp"
android:prompt="@string/touch" />
貼出圓角的檔案代碼,tipspriceshape.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的顔色 -->
<solid android:color="#2B2B2B" />
<!-- 設定按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:radius="@dimen/RoundedAmplitude" />
<gradient
android:angle="270"
android:centerColor="#2B2B2B"
android:endColor="#2B2B2B"
android:startColor="#2B2B2B" />
<stroke
android:width="0.01dp"
android:color="#FFFFFF">
</stroke>
</shape>
這樣就順利的完成了需求,改變了預設的樣式為圓角,并且文字居中,顔色為白色。截圖如下:
(紅色的為了保密所需要,歡迎各位前輩指出不足,或者更好的辦法)。
=========================分割線,2015年12月18日10:27:04=====================================================
增加一個spinner的功能,當使用者點選某一個想要修改的時候,進入到spinner的頁面,需要顯示上次修改的清單值,示例代碼如下:
@Override
public void onDataEvent(final StationEventData data) {//回調接口,不明白的,可以參考我之前的部落格
if (data.getEventName()
.equals(IStationEventName.EVENT_NAME_TIME_CHANGE)
&& !isHidden()) {
if (timeView != null) {
Runnable work = new Runnable() {
@Override
public void run() {
if (data.getData().toString().equals("1")) {
timeView.setHint("請重新選擇有效日期");
expireTimeSpinner.setSelection(8);
} else {
timeView.setText(data.getData().toString() + ":00");
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(data.getData().toString());
} catch (ParseException e) {
e.printStackTrace();
}
effectivetime = date;
}
}
};
getHandler().post(work);
}
} else if (data.getEventName().equals(
IStationEventName.EVENT_NAME_TIME_CHANGE_MODIFY)
&& getActivity() != null) {
// 2015年12月18日10:21:36:當使用者點選修改某個價格,用來設定下拉清單預設現實的類型
int typeselect = Integer.parseInt(data.getData().toString());
if (typeselect == 0) {
expireTimeSpinner.setSelection(8);
} else if (typeselect == 1) {
expireTimeSpinner.setSelection(6);
} else if (typeselect == 2) {
<span style="color:#ff0000;">expireTimeSpinner.setSelection(7);</span>
} else {
expireTimeSpinner.setSelection(9);
}
}
}