android 日期控件分析

无情 阅读:247 2021-03-31 23:31:22 评论:0

相关布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
     
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:background="#CDC5BF" 
         
        > 
        <EditText 
            android:id="@+id/data" 
            android:layout_weight="1" 
            android:layout_height="42dp" 
            android:layout_width="fill_parent" 
            android:hint="在此输入查询时间" 
            android:textSize="12dp"  
            android:layout_marginLeft="8dp" 
            android:layout_marginRight="4dp" 
           android:background="@drawable/shape" 
            android:layout_marginTop="2dp" 
            android:layout_marginBottom="2dp" 
            android:gravity="center" 
            ></EditText> 
        <EditText 
            android:id="@+id/address" 
            android:layout_weight="1" 
            android:layout_height="42dp" 
            android:layout_width="fill_parent" 
            android:hint="在此输入查询支行" 
            android:textSize="12dp" 
            android:layout_marginLeft="4dp" 
            android:layout_marginRight="8dp"  
            android:layout_marginTop="2dp" 
            android:layout_marginBottom="2dp" 
             android:background="@drawable/shape" 
             android:gravity="center" 
            ></EditText> 
         
    </LinearLayout> 
 
    <ListView 
        android:id="@+id/listview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1.0" /> 
 
</LinearLayout>


Activity源代码:

package com.activity.gdbk; 
 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.os.Vibrator; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.AdapterView.OnItemLongClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 
 
//Android ListVew 点击item 显示隐匿菜单项 
public class ListActivity extends Activity implements OnItemClickListener, 
		OnItemLongClickListener { 
	// 自定义相关组件 
	private ListView listview; 
	private MyAdapter adapter; 
	// activity 数据源 
	private List<String> data; 
 
	// activity 标量 
	private int currentPosition = -1; 
	// activity 界面常量 
	private static final int DATE_DIALOG_ID = 1; 
	private static final int SHOW_DATAPICK = 0; 
	// 日期数据变量 
	private int mYear; 
	private int mMonth; 
	private int mDay; 
 
	// ListActivity 相关组件 
	private EditText time; 
 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		// 全屏模式 
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.activity_listview); 
		initView(); 
		setData(); 
	} 
 
	// 自定义相关方法 
	private void initView() { 
		// 组件实例化 
		listview = (ListView) findViewById(R.id.listview); 
		time = (EditText) findViewById(R.id.data); 
		// 默认日期 
		setDateTime(); 
 
		// 设置相关监听事件 
		listview.setOnItemClickListener(this); 
		listview.setOnItemLongClickListener(this); 
 
		time.setOnClickListener(new DateButtonOnClickListener()); 
	} 
 
	private void setDateTime() { 
		final Calendar c = Calendar.getInstance(); 
		mYear = c.get(Calendar.YEAR); 
		mMonth = c.get(Calendar.MONTH); 
		mDay = c.get(Calendar.DAY_OF_MONTH); 
		updateDisplay(); 
	} 
 
	/** 
	 * 更新日期 
	 */ 
	private void updateDisplay() { 
		time.setText(new StringBuilder().append(mYear).append("--") 
				.append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("--") 
				.append((mDay < 10) ? "0" + mDay : mDay)); 
	} 
 
	// 设置相关静态数据 
	private void setData() { 
		data = new ArrayList<String>(); 
		for (int i = 0; i < 10; i++) { 
			data.add("员工姓名" + i); 
		} 
		if (adapter == null) { 
			adapter = new MyAdapter(this, data); 
			listview.setAdapter(adapter); 
		} else { 
			adapter.notifyDataSetChanged(); 
		} 
	} 
 
	@Override 
	protected void onResume() { 
		updateNoteBookList(); 
		super.onResume(); 
	} 
 
	private void updateNoteBookList() { 
		if (adapter != null) { 
			setData(); 
			adapter.notifyDataSetChanged(); 
		} 
	} 
 
	// 覆写相关方法 
	@Override 
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
			int position, long arg3) { 
		// TODO Auto-generated method stub 
		Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 
		vibrator.vibrate(40); 
		currentPosition = position; 
		adapter.notifyDataSetChanged(); 
		return true; 
	} 
 
	@Override 
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
		// TODO Auto-generated method stub 
		currentPosition = -1; 
		adapter.notifyDataSetChanged(); 
	} 
 
	// 内部类---自定义适配器 
	public class MyAdapter extends BaseAdapter { 
		private Context context; 
		private LayoutInflater inflater; 
		private List<String> fileTypeList; 
 
		// 适配器构造函数 
		public MyAdapter(Context context, List<String> fileTypeList) { 
			this.context = context; 
			this.fileTypeList = fileTypeList; 
			inflater = LayoutInflater.from(context); 
		} 
 
		@Override 
		public int getCount() { 
			// TODO Auto-generated method stub 
			return fileTypeList.size(); 
		} 
 
		@Override 
		public Object getItem(int arg0) { 
			// TODO Auto-generated method stub 
			return fileTypeList.get(arg0); 
		} 
 
		@Override 
		public long getItemId(int position) { 
			// TODO Auto-generated method stub 
			return position; 
		} 
 
		@Override 
		public View getView(int position, View convertView, ViewGroup parent) { 
			// TODO Auto-generated method stub 
			ViewHolder holder = null; 
			if (convertView == null) { 
				convertView = inflater.inflate(R.layout.listview_item, parent, 
						false); 
				holder = new ViewHolder(); 
				holder.fileNameText = (TextView) convertView 
						.findViewById(R.id.item_name); 
				holder.fileTimeText = (TextView) convertView 
						.findViewById(R.id.item_time); 
				holder.fileSizeText = (TextView) convertView 
						.findViewById(R.id.item_pm); 
				holder.linearLayout = (LinearLayout) convertView 
						.findViewById(R.id.layout_other); 
				holder.openLinearLayout = (LinearLayout) convertView 
						.findViewById(R.id.item_open); 
				holder.editLinearLayout = (LinearLayout) convertView 
						.findViewById(R.id.item_edit); 
				holder.moveLinearLayout = (LinearLayout) convertView 
						.findViewById(R.id.item_move); 
				holder.deleteLinearLayout = (LinearLayout) convertView 
						.findViewById(R.id.item_delete); 
				convertView.setTag(holder); 
			} else { 
				holder = (ViewHolder) convertView.getTag(); 
			} 
			// 填充相关数据信息 
			String strs = fileTypeList.get(position); 
			holder.fileNameText.setText(strs); 
			holder.fileTimeText.setText(""); 
			holder.fileSizeText.setText(""); 
 
			// item 显示隐藏菜单 
			if (position == currentPosition) { 
				holder.linearLayout.setVisibility(View.VISIBLE); 
				holder.openLinearLayout.setClickable(true); 
				holder.editLinearLayout.setClickable(true); 
				holder.moveLinearLayout.setClickable(true); 
				holder.deleteLinearLayout.setClickable(true); 
				holder.openLinearLayout 
						.setOnClickListener(new OnClickListener() { 
 
							@Override 
							public void onClick(View v) { 
								// 饼状图页面 
 
								Intent pie = new Intent(ListActivity.this, 
										PieActivity.class); 
								startActivity(pie); 
							} 
						}); 
				holder.editLinearLayout 
						.setOnClickListener(new OnClickListener() { 
 
							@Override 
							public void onClick(View v) { 
								// TODO Auto-generated method stub 
								// 柱状图页面 
 
								Intent intent = new Intent(ListActivity.this, 
										BarActivity.class); 
								startActivity(intent); 
 
							} 
						}); 
				holder.moveLinearLayout 
						.setOnClickListener(new OnClickListener() { 
 
							@Override 
							public void onClick(View v) { 
								// 散点图页面 
 
								Intent scatter = new Intent(ListActivity.this, 
										ScatterAtivity.class); 
								startActivity(scatter); 
 
							} 
						}); 
				holder.deleteLinearLayout 
						.setOnClickListener(new OnClickListener() { 
 
							@Override 
							public void onClick(View v) { 
								// TODO Auto-generated method stub 
								// //折线图页面 
 
								Intent linear = new Intent(ListActivity.this, 
										LinearActivity.class); 
								startActivity(linear); 
 
							} 
						}); 
			} else { 
				holder.linearLayout.setVisibility(View.GONE); 
				holder.openLinearLayout.setClickable(false); 
				holder.editLinearLayout.setClickable(false); 
				holder.moveLinearLayout.setClickable(false); 
				holder.deleteLinearLayout.setClickable(false); 
			} 
 
			return convertView; 
		} 
 
	} 
 
	// 内部类-----自定义组件 
 
	public class ViewHolder { 
		public TextView fileNameText; 
		public TextView fileTimeText; 
		public TextView fileSizeText; 
		public LinearLayout linearLayout; 
		public LinearLayout openLinearLayout; 
		public LinearLayout editLinearLayout; 
		public LinearLayout moveLinearLayout; 
		public LinearLayout deleteLinearLayout; 
 
	} 
 
	// 自定义 监听事件 
	/** 
	 * 选择日期Button的事件处理 
	 *  
	 * @author Raul 
	 *  
	 */ 
 
	class DateButtonOnClickListener implements 
 
	android.view.View.OnClickListener { 
 
		@Override 
		public void onClick(View v) { 
 
			Message msg = new Message(); 
 
			if (time.equals((EditText) v)) { 
 
				msg.what = ListActivity.SHOW_DATAPICK; 
 
			} 
 
			ListActivity.this.saleHandler.sendMessage(msg); 
 
		} 
 
	} 
 
	// ListActivity 主线程 
	/** 
	 * 处理日期控件的Handler 
	 */ 
 
	Handler saleHandler = new Handler() { 
		@Override 
		public void handleMessage(Message msg) { 
			switch (msg.what) { 
			case ListActivity.SHOW_DATAPICK: 
				showDialog(DATE_DIALOG_ID); 
				break; 
			} 
		} 
	}; 
 
	/** 
	 * 日期控件的事件 
	 */ 
 
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 
		public void onDateSet(DatePicker view, int year, int monthOfYear, 
				int dayOfMonth) { 
			mYear = year; 
			mMonth = monthOfYear; 
			mDay = dayOfMonth; 
			updateDisplay(); 
 
		} 
 
	}; 
 
	@Override 
	protected Dialog onCreateDialog(int id) { 
		switch (id) { 
		case DATE_DIALOG_ID: 
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
					mDay); 
 
		} 
		return null; 
	} 
 
	@Override 
	protected void onPrepareDialog(int id, Dialog dialog) { 
		switch (id) { 
		case DATE_DIALOG_ID: 
			((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 
			break; 
		} 
	} 
 
} 

效果展示:


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号