public class MainActivity extends ActionBarActivity {
CalendarView view;
private AlertDialog.Builder builder;
private AlertDialog dialog;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.bt_1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
builder = new Builder(MainActivity.this,
AlertDialog.THEME_HOLO_DARK);
final LayoutInflater inflater = LayoutInflater
.from(MainActivity.this);
LinearLayout.LayoutParams relalin = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout mlin = new LinearLayout(MainActivity.this);
mlin.setLayoutParams(relalin);
LinearLayout.LayoutParams rela = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rela.topMargin = 10;
rela.bottomMargin = 10;
TextView tv = new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(26);
tv.setText("日期选择");
mlin.addView(tv, rela);
// 设置标题
builder.setCustomTitle(mlin);
// 自定义布局
View dialogv = inflater.inflate(R.layout.data, null);
CalendarView cav = (CalendarView) dialogv.findViewById(R.id.calendarView1);
cav.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth) {
// TODO Auto-generated method stub
//这里就是获取得到年月日的信息 月要加一
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dia, int which) {
// TODO Auto-generated method stub
//这里点击后进行逻辑判断
}
});
builder.setNegativeButton("取消", null);
builder.setView(dialogv);
builder.create();
dialog = builder.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
main.xml的布局文件就一个按钮
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_1"
android:text="点击"/>
data.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:gravity="center">
<CalendarView
android:id="@+id/calendarView1"
android:background="@color/lucency"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>
基本上就这样。反正我是花了很长时间才看懂只是不想忘记就记下来了 第一次用简书还是有点小激动!
http://pan.baidu.com/s/1skAxcOd