Creating Date Objects
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *tomorrow = [NSDate
dateWithTimeIntervalSinceNow:secondsPerDay];
NSDate *yesterday = [NSDate
dateWithTimeIntervalSinceNow:-secondsPerDay];
To get new date objects with date-and-time values adjusted from existing date objects, use addTimeInterval:.
NSTimeInterval secondsPerDay = 24 * 60 * 60; NSDate *today = [NSDate date]; NSDate *tomorrow, yesterday; tomorrow = [today addTimeInterval:secondsPerDay]; yesterday = [today addTimeInterval:-secondsPerDay];
Basic Date Calculations
To compare dates, use the isEqualToDate:, compare:, laterDate:, and earlierDate: methods. These methods perform exact comparisons, which means they will detect subsecond differences between dates. You might want to compare dates with a less fine granularity. For example, you might want to consider two dates equal if they are within a minute of each other. If this is the case, use timeIntervalSinceDate: to compare the two dates or use a Gregorian date instead (NSCalendarDate in Objective-C, NSGregorianDate in Java). The following Objective-C code shows how to use timeIntervalSinceDate: to see if two dates are within one minute (60 seconds) of each other.
if (fabs([date2 timeIntervalSinceDate:date1]) < 60) ...
Android docs中的范例,简单翻译整理如下。
--------------------------------------------
1、创建新项目,名称HelloDatePicker;
2、修改res/layout/main.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/dateDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<Button android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change the date"/>
</LinearLayout>
3、修改HelloDatePicker.java,增加成员变量定义,代码如下:
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
4、修改HelloDatePicker.java的onCreate方法,内容如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// capture our View elements
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
// add a click listener to the button
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDisplay();
}
5、修改HelloDatePicker.java增加updateDisplay方法,内容如下:
// updates the date in the TextView
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
6、修改HelloDatePicker.java增加日期选择对话框,内容如下:
// the callback received when the user "sets" the date in the dialog
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();
}
};
7、修改HelloDatePicker.java增加onCreateDialog方法,内容如下:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
8、运行项目,即可看到运行效果,如下图所示:
虽然功能上实现了但是布局上不够漂亮,很丑啊,考虑用网页的方式来实现,思路如果可以借鉴请借鉴。作用主要是动态生成一些页面控件,大家有好的思路也可以提供给我啊。或者修改这段代码,layout.xml方式暂时不考虑
typeindex = 0;
adindex = 0;
for(int i = 0; i <list.size();i++){
final int tempint = i;
String flag = list.get(i).getFlg();
RelativeLayout rel = new RelativeLayout(Act.this);
rel.setGravity(Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if("1".equals(flag)){
TextView t = new TextView(Act.this);
t.setText("类 型:");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
RelativeLayout rel2 = new RelativeLayout(Act.this);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText tt = new EditText(Act.this);
tt.setBackgroundDrawable(getResources().getDrawable(R.drawable.select_edit));
tt.setEnabled(false);
tt.setSingleLine();
tt.setId(2);
ImageView image1 = new ImageView(Act.this);
image1.setImageDrawable(getResources().getDrawable(R.drawable.select));
image1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<SelectBean> selectList = list.get(tempint).getList();
final String[] names = new String[selectList.size()];
final String[] ids = new String[selectList.size()];
for(int i=0;i<selectList.size();i++){
names[i] = selectList.get(i).getName();
ids[i] = selectList.get(i).getId();
}
dialog = new AlertDialog.Builder(Act.this)
.setTitle("请选择类型")
.setSingleChoiceItems(names, typeindex, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
typeindex = whichButton;
}
}).setPositiveButton("确 定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
tt.setText(names[typeindex]);
typeStr = "&"+list.get(tempint).getArg_name()+"="+ids[typeindex];
}
})
.setNegativeButton("取 消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).create();
dialog.show();
}
});
rel2.addView(tt);
lp2.addRule(RelativeLayout.RIGHT_OF, tt.getId());
rel2.addView(image1,lp2);
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(rel2,lp);
}else if("2".equals(flag)){
TextView t = new TextView(Act.this);
t.setText("前 缀:");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
RelativeLayout rel2 = new RelativeLayout(Act.this);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText tt = new EditText(Act.this);
tt.setBackgroundDrawable(getResources().getDrawable(R.drawable.select_edit));
tt.setEnabled(false);
tt.setSingleLine();
tt.setId(2);
ImageView image1 = new ImageView(Act.this);
image1.setImageDrawable(getResources().getDrawable(R.drawable.select));
image1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<SelectBean> selectList1 = list.get(tempint).getList();
final String[] namess = new String[selectList1.size()];
final String[] idss = new String[selectList1.size()];
for(int i=0;i<selectList1.size();i++){
namess[i] = selectList1.get(i).getName();
idss[i] = selectList1.get(i).getId();
}
dialog = new AlertDialog.Builder(Act.this)
.setTitle("前 缀:")
.setSingleChoiceItems(namess, adindex, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
adindex = whichButton;
}
}).setPositiveButton("确 定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
tt.setText(namess[adindex]);
adStrx = namess[adindex];
adStr = "&"+list.get(tempint).getArg_name()+"="+idss[adindex];
}
})
.setNegativeButton("取 消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).create();
dialog.show();
}
});
rel2.addView(tt);
lp2.addRule(RelativeLayout.RIGHT_OF, tt.getId());
rel2.addView(image1,lp2);
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(rel2,lp);
}else if("3".equals(flag)){
TextView t = new TextView(Act.this);
t.setText("号 :");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
tt3 = new EditText(Act.this);
tt3.setBackgroundDrawable(getResources().getDrawable(R.drawable.text));
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(tt3,lp);
}else if("4".equals(flag)){
TextView t = new TextView(Act.this);
t.setText(" 号:");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
tt4 = new EditText(Act.this);
tt4.setBackgroundDrawable(getResources().getDrawable(R.drawable.text));
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(tt4,lp);
}else if("5".equals(flag)){
TextView t = new TextView(Act.this);
t.setText("代 码:");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
tt5 = new EditText(Act.this);
tt5.setBackgroundDrawable(getResources().getDrawable(R.drawable.text));
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(tt5,lp);
}else if("6".equals(flag)){
TextView t = new TextView(Act.this);
t.setText("编 号:");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
tt6 = new EditText(Act.this);
tt6.setBackgroundDrawable(getResources().getDrawable(R.drawable.text));
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(tt6,lp);
}else if("9".equals(flag)){
TextView t = new TextView(Act.this);
t.setText("号: ");
t.setTextColor(R.color.define_blue);
t.setTextSize(18);
t.setId(1);
tt9 = new EditText(Act.this);
tt9.setBackgroundDrawable(getResources().getDrawable(R.drawable.text));
lp.addRule(RelativeLayout.RIGHT_OF,t.getId());
rel.addView(t);
rel.addView(tt9,lp);
}
linear.addView(rel);
}
}