文件名称:Android 日期控件只显示年月
文件大小:152KB
文件格式:ZIP
更新时间:2016-09-17 11:25:17
日期控件
public class Android_DateActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button button; private TextView textview; private Dialog mdialog; private Calendar calendar = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); initUi(); button.setOnClickListener(this); } public void initUi() { button = (Button) findViewById(R.id.button); textview = (TextView) findViewById(R.id.textview); } @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0);// 日期弹出框 int SDKVersion = Android_DateActivity.this.getSDKVersionNumber();// 获取系统版本 System.out.println("SDKVersion = " + SDKVersion); DatePicker dp = findDatePicker((ViewGroup) mdialog.getWindow() .getDecorView());// 设置弹出年月日 if (dp != null) { if (SDKVersion < 11) { ((ViewGroup) dp.getChildAt(0)).getChildAt(1).setVisibility( View.GONE); } else if (SDKVersion > 14) { ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)) .getChildAt(1).setVisibility(View.GONE); } } } @Override protected Dialog onCreateDialog(int id) { // 对应上面的showDialog(0);//日期弹出框 mdialog = null; switch (id) { case 0: calendar = Calendar.getInstance(); mdialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { textview.setText(year + "-" + (monthOfYear + 1)); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); break; } return mdialog; } /** * 从当前Dialog中查找DatePicker子控件 * * @param group * @return */ private DatePicker findDatePicker(ViewGroup group) { if (group != null) { for (int i = 0, j = group.getChildCount(); i < j; i++) { View child = group.getChildAt(i); if (child instanceof DatePicker) { return (DatePicker) child; } else if (child instanceof ViewGroup) { DatePicker result = findDatePicker((ViewGroup) child); if (result != null) return result; } } } return null; } /** * 获取系统SDK版本 * * @return */ public static int getSDKVersionNumber() { int sdkVersion; try { sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK); } catch (NumberFormatException e) { sdkVersion = 0; } return sdkVersion; } }
【文件预览】:
Android_Date
----bin()
--------classes.dex(6KB)
--------res()
--------jarlist.cache(119B)
--------classes()
--------resources.ap_(34KB)
--------Android_Date.apk(38KB)
----res()
--------drawable-ldpi()
--------drawable-hdpi()
--------drawable-xhdpi()
--------values()
--------drawable-mdpi()
--------layout()
----proguard-project.txt(781B)
----assets()
----gen()
--------com()
----src()
--------com()
----.project(848B)
----.classpath(364B)
----project.properties(563B)
----AndroidManifest.xml(737B)