Android 中日期控件与下拉列表的使用

时间:2022-05-19 18:05:46

1、在一个EditText 中单击一下可以弹出日期对话框:

    private static int START;
private int mYear;
private int mMonth;
private int mDay;

    private class SetParaImpl implements OnClickListener {

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_time:
START = 1;
new DatePickerDialog(MainActivity.this, mDateSetListener, mYear, mMonth, mDay).show();
break;
case R.id.end_time:
START = 2;
new DatePickerDialog(MainActivity.this, mDateSetListener, mYear, mMonth, mDay).show();
break;
case R.id.set_param:
startActivity(new Intent(MainActivity.this, EnvironmentSetActivity.class));
break;
case R.id.query:
startActivity(new Intent(MainActivity.this, AchartengineActivity.class));
break;
default:
break;
}
}

}

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
if (START == 1) {
JavaUtils.updateDisplay(START, startTime, mYear, mMonth, mDay);
} else if (START == 2) {
JavaUtils.updateDisplay(START, endTime, mYear, mMonth, mDay);
}
}

};

2、
public class JavaUtils {
public static void updateDisplay(int start, EditText view, int mYear, int mMonth, int mDay) {
if (start == 1) {

view.setText(new StringBuilder().append(mYear).append("-").append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-").append((mDay + 1) < 10 ? "0" + (mDay) : (mDay)));
} else {
view.setText(new StringBuilder().append(mYear).append("-").append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-").append((mDay + 1) < 10 ? "0" + (mDay) : (mDay)));
}

}
}

3、下拉列表的使用:

            condition = (Spinner) view.findViewById(R.id.condition);
adapterItem = ArrayAdapter.createFromResource(GreenHouseActivity.this, R.array.condition, android.R.layout.simple_spinner_item); // 从资源文件中读取选项内容

adapterItem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 设置列表显示风格
condition.setAdapter(adapterItem); // 设置下拉列表选项

condition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
android.util.Log.d("mark", "p=" + position);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
});

note:R.array.condition 放置在values 文件下的array.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="condition">
<item>月</item>
<item>周</item>
<item>天</item>
<item>6小时</item>
</string-array>
</resources>

自定义下拉列表:

    private void homeSeceneMode() {
ArrayAdapter<CharSequence> adapterMode = null; // 下拉列表内容适配器

List<CharSequence> data = new CURDTable(this).allSeceneMode();
if (data != null) {
adapterMode = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, data);
adapterMode.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 设置下拉列表显示风格
this.homeSeceneMode.setAdapter(adapterMode);
}
}

    public List<CharSequence> allSeceneMode() {
db = this.helper.getReadableDatabase();
List<CharSequence> info = new ArrayList<CharSequence>();
String sql = "select name from secene_mode_tb";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {

info.add(cursor.getString(cursor.getColumnIndex("name")));
}
db.close();
return info;
}