android中的calendarview问题 [英] Issue on calendarview in android
本文介绍了android中的calendarview问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从日历中选择后我无法阅读日期。实际上我没有以正常方式显示日历,我在弹出窗口显示日历视图,工作正常。
当我试图处理时,
日历视图应用程序的单击事件已崩溃。我不明白该怎么做。请帮助我在EdiText1中显示所选日期。
xml文件activity_home1是
I am not able to read the date after selecting from calendar.Actually i didt show the calendar in normal way,i displayed calendar view in popup window its working fine.
when i tried to handle the click event of calendar view application is crashed.i am not understanding what to do.please help me to display selected date into EdiText1.
xml file activity_home1 is
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:removed="#CCC"
android:orientation="vertical" >
<Button
android:id="@+id/show_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup" />
<edittext>
android:id="@+id/editText1fordate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestfocus />
</edittext>
</linearlayout>
< br $>
Myactivity类是:
Myactivity class is:
package com.annomap.datepicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.annomap.datepicker.R;
import com.annomap.datepicker.R.string;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class Home1Activity extends Activity {
CalendarView cal;
Point p;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.show_popup);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home1);
cal = (CalendarView) findViewById(R.id.calendarView1);
cal.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
c.set(year, month, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd");
String formatteddate = sdf.format(c.getTime());
EditText editext1 = (EditText) findViewById(R.id.calendarView1);
editext1.setText(formatteddate);
}
});
Button btn_show = (Button) findViewById(R.id.show_popup);
btn_show.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Open popup window
if (p != null)
showPopup(Home1Activity.this, p);
}
private void showPopup(Activity context, Point p) {
// TODO Auto-generated method stub
int popupWidth = 200;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup1);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout,
viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit
// down, relative to button's position.
int OFFSET_X = 110;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY,
p.x + OFFSET_X, p.y + OFFSET_Y);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.home1, menu);
return true
}
}
推荐答案
如果没有看到你的LogCat异常消息很难帮助你,你可以更好地使用这个适合你需要的github项目:
CalendarDialogBuilder [ ^ ]
Its hard to help you without seeing your LogCat exception message, better you can this github project which will suit your need:
CalendarDialogBuilder[^]
这篇关于android中的calendarview问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文