日期选择器
1、在main.xml里添加一个跳入日期选择的Activity的按钮
<Button android:id="@+id/date_picker_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DatePicker"/>
2、在MainActivity.java里写该按钮的监听器 只是黄色标记的代码,其他的是以前写的
package zeng.Demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(button_listener);
TextView text_view_button=(TextView)findViewById(R.id.text_view_button);
text_view_button.setOnClickListener(text_view_button_listener);
Button spinner_button = (Button)findViewById(R.id.spinner_button);
spinner_button.setOnClickListener(spinner_button_listener);
Button auto_complete_button=(Button)findViewById(R.id.auto_complete_button);
auto_complete_button.setOnClickListener(auto_complete_button_listener);
Button data_picker_button=(Button)findViewById(R.id.date_picker_button);
data_picker_button.setOnClickListener(date_picker_button_listener);
}
private Button.OnClickListener button_listener = new OnClickListener(){
public void onClick(View v){
setTitle("button被点击了一下");
}
};
private Button.OnClickListener text_view_button_listener = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this,TextActivity.class);
startActivity(intent);
}
};
private Button.OnClickListener check_box_button_listener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this,CheckBoxActivity.class);
startActivity(intent);
}
};
private Button.OnClickListener spinner_button_listener=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, SpinnerActivity.class);
startActivity(intent);
}
};
private Button.OnClickListener auto_complete_button_listener = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, AutoCompleteTextViewActivity.class);
startActivity(intent);
}
};
private Button.OnClickListener date_picker_button_listener = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this,DatePickerActivity.class);
startActivity(intent);
}
};
}
3、在Layout里新建一个xml datepicker.xml 用于显示日期选择器控件的
<DatePicker android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
4、新建一个DatePickerActivity.java,
package zeng.Demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
public class DatePickerActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle("DatePickerActivity");
setContentView(R.layout.datepicker);
DatePicker dp =(DatePicker)this.findViewById(R.id.date_picker);
dp.init(2009, 5, 11, null);
}
}
5、在AndroidManifest.xml里注册
<activity android:name=".DatePickerActivity">
</activity>
6、最后的显示