您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了Android如何实现仿iPhone日期时间选择器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
先看效果图
如何使用
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { WheelMain wheelMain; EditText txttime; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txttime = (EditText) findViewById(R.id.txttime); Calendar calendar = Calendar.getInstance(); txttime.setText(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH) + ""); Button btnselecttime = (Button) findViewById(R.id.button1); btnselecttime.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { LayoutInflater inflater = LayoutInflater .from(MainActivity.this); final View timepickerview = inflater.inflate( R.layout.timepicker, null); ScreenInfo screenInfo = new ScreenInfo(MainActivity.this); wheelMain = new WheelMain(timepickerview, true); wheelMain.screenheight = screenInfo.getHeight(); String time = txttime.getText().toString(); Calendar calendar = Calendar.getInstance(); if (JudgeDate.isDate(time, "yyyy-MM-dd")) { try { calendar.setTime(dateFormat.parse(time)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int h = calendar.getTime().getHours(); int m = calendar.getTime().getMinutes(); wheelMain.initDateTimePicker(year, month, day, h, m); new AlertDialog.Builder(MainActivity.this) .setTitle("选择时间") .setView(timepickerview) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { txttime.setText(wheelMain.getTime()); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } }); } }
感谢你能够认真阅读完这篇文章,希望小编分享的“Android如何实现仿iPhone日期时间选择器”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。