編輯:關於Android編程
//點擊日期按鈕布局 設置日期 layoutDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { // TODO Auto-generated method stub mYear = year; mMonth = month; mDay = day; //更新EditText控件日期 小於10加0 dateEdit.setText(new StringBuilder().append(mYear).append(-) .append((mMonth + 1) < 10 ? 0 + (mMonth + 1) : (mMonth + 1)) .append(-) .append((mDay < 10) ? 0 + mDay : mDay) ); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) ).show(); } });其中mYear、mMonth、mDay是定義變量,同時dateEdit是一個EditText控件用於顯示具體日期,當數字小於10時前補0,如2015-01-01。下面是設置時間,顯示效果和第三部分的效果相同。
//點擊時間按鈕布局 設置時間 layoutTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hour, int minute) { // TODO Auto-generated method stub mHour = hour; mMinute = minute; //更新EditText控件時間 小於10加0 timeEdit.setText(new StringBuilder() .append(mHour < 10 ? 0 + mHour : mHour).append(:) .append(mMinute < 10 ? 0 + mMinute : mMinute).append(:00) ); } }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show(); } });
3.date_dialog.xml文件:日期控件布局
4.time_dialog.xml文件:時間控件布局
5.drawable-hdpi中editview_shape.xml文件 實現自定義EditText控件格式
6.MainActivity.java源碼
public class MainActivity extends Activity { //自定義變量 private EditText titleEdit; private EditText dateEdit; private EditText timeEdit; private EditText contentEdit; //底部四個布局按鈕 private LinearLayout layoutDate; private LinearLayout layoutTime; private LinearLayout layoutCancel; private LinearLayout layoutSave; //定義顯示時間控件 private Calendar calendar; //通過Calendar獲取系統時間 private int mYear; private int mMonth; private int mDay; private int mHour; private int mMinute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //鎖定屏幕 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.activity_main); //獲取對象 titleEdit = (EditText) findViewById(R.id.showtitle); dateEdit = (EditText) findViewById(R.id.showdate); timeEdit = (EditText) findViewById(R.id.showtime); contentEdit = (EditText) findViewById(R.id.editText1); layoutDate = (LinearLayout) findViewById(R.id.layout_date); layoutTime = (LinearLayout) findViewById(R.id.layout_time); layoutCancel = (LinearLayout) findViewById(R.id.layout_cancel); layoutSave = (LinearLayout) findViewById(R.id.layout_save); calendar = Calendar.getInstance(); //點擊日期按鈕布局 設置日期 layoutDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //通過自定義控件AlertDialog實現 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View view = (LinearLayout) getLayoutInflater().inflate(R.layout.date_dialog, null); final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker); //設置日期簡略顯示 否則詳細顯示 包括:星期周 datePicker.setCalendarViewShown(false); //初始化當前日期 calendar.setTimeInMillis(System.currentTimeMillis()); datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), null); //設置date布局 builder.setView(view); builder.setTitle(設置日期信息); builder.setPositiveButton(確 定, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //日期格式 StringBuffer sb = new StringBuffer(); sb.append(String.format(%d-%02d-%02d, datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth())); dateEdit.setText(sb); //賦值後面鬧鐘使用 mYear = datePicker.getYear(); mMonth = datePicker.getMonth(); mDay = datePicker.getDayOfMonth(); dialog.cancel(); } }); builder.setNegativeButton(取 消, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create().show(); } }); layoutDate.setOnTouchListener(new OnTouchListener() { //設置布局背景 @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { layoutDate.setBackgroundColor(Color.WHITE); layoutTime.setBackgroundColor(Color.TRANSPARENT); layoutCancel.setBackgroundColor(Color.TRANSPARENT); layoutSave.setBackgroundColor(Color.TRANSPARENT); } return false; } }); //點擊時間按鈕布局 設置時間 layoutTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //自定義控件 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View view = (LinearLayout) getLayoutInflater().inflate(R.layout.time_dialog, null); final TimePicker timePicker = (TimePicker) view.findViewById(R.id.time_picker); //初始化時間 calendar.setTimeInMillis(System.currentTimeMillis()); timePicker.setIs24HourView(true); timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); timePicker.setCurrentMinute(Calendar.MINUTE); //設置time布局 builder.setView(view); builder.setTitle(設置時間信息); builder.setPositiveButton(確 定, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mHour = timePicker.getCurrentHour(); mMinute = timePicker.getCurrentMinute(); //時間小於10的數字 前面補0 如01:12:00 timeEdit.setText(new StringBuilder().append(mHour < 10 ? 0 + mHour : mHour).append(:) .append(mMinute < 10 ? 0 + mMinute : mMinute).append(:00) ); dialog.cancel(); } }); builder.setNegativeButton(取 消, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create().show(); } }); layoutTime.setOnTouchListener(new OnTouchListener() { //設置布局背景 @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { layoutDate.setBackgroundColor(Color.TRANSPARENT); layoutTime.setBackgroundColor(Color.WHITE); layoutCancel.setBackgroundColor(Color.TRANSPARENT); layoutSave.setBackgroundColor(Color.TRANSPARENT); } return false; } }); //點擊取消按鈕 layoutCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dateEdit.setText(); dateEdit.setHint(2015-01-01); timeEdit.setText(); timeEdit.setHint(00:00:00); contentEdit.setText(); contentEdit.setHint(記錄旅途中的備注信息...); } }); layoutCancel.setOnTouchListener(new OnTouchListener() { //設置布局背景 @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { layoutDate.setBackgroundColor(Color.TRANSPARENT); layoutTime.setBackgroundColor(Color.TRANSPARENT); layoutCancel.setBackgroundColor(Color.WHITE); layoutSave.setBackgroundColor(Color.TRANSPARENT); } return false; } }); //點擊保存按鈕 layoutSave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //確認保存按鈕 new AlertDialog.Builder(MainActivity.this).setTitle(確認保存嗎?) .setIcon(android.R.drawable.ic_dialog_info) .setPositiveButton(確 定, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { /** * 數據庫插入操作 */ } }) .setNegativeButton(返 回, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } }); layoutSave.setOnTouchListener(new OnTouchListener() { //設置布局背景 @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { layoutDate.setBackgroundColor(Color.TRANSPARENT); layoutTime.setBackgroundColor(Color.TRANSPARENT); layoutCancel.setBackgroundColor(Color.TRANSPARENT); layoutSave.setBackgroundColor(Color.WHITE); } return false; } }); } // End onCreate }最後希望文章對大家有所幫助!如果你知道我這篇文章想要闡述的內容是什麼?為什麼要寫這篇文章?或許它會對你有所幫助,一方面是布局可能對你有啟發;另一方面就是剛好遇到那個問題的同學。
在購買商品時,大家可以自定義數字加減控件,來確定購買商品的實際數量,如何實現此控件,請參考下文:1.自定義數字加減控件的要求創建Module -NumberAddSubV
一、配置NDK環境第一步:在AndroidStudio中配置ndk環境需要下載ndk包,在AndroidStudio中File-->ProjectStructure
一、前言Android應用開發中多線程編程應用比較廣泛,而應用比較多的是ThreadPoolExecutor,AsyncTask,IntentService,Handle
前言:前面兩篇分別介紹了:Android-Universal-Image-Loader (圖片異步加載緩存庫)的使用配置Android-Universal-Image-L