wake-up-neo.net

Benutzerdefinierte Kalenderansicht in Android

Ich möchte eine benutzerdefinierte Kalenderansicht für Android OS 1.5 und höher entwickeln.

Ähnlich wie Android-Tageskalender und Ereignis hinzufügen und in der Tagesansicht anzeigen.

Wenn Sie ein Beispiel oder eine Quelle davon haben, geben Sie mir bitte.

Ich habe keine Ahnung, wie ich anfangen soll. Bitte führen Sie mich.

Ich habe die Monatsansicht unter folgendem Link durchgeführt: 

http://w2davids.wordpress.com/Android-simple-calendar/

aber ich muss auch eine tagesansicht erstellen, also bitte helft mir.

Ich möchte das anzeigen: 

enter image description here

17
Nikhil

Ich habe gerade daran gearbeitet:

Preview

Man könnte es als Blaupause betrachten.

/**
 * @author Sherif
 * 
 * Copyright 2011
 *
 * Sample Day Viewer that will show entries of each hour with ability to 
 * add events and stuff
 * You should find a way to keep a container that will keep track of added events
 *
 */
public class DayViewActivity extends ListActivity {
/** Called when the activity is first created. */
private static int HOURS_PER_DAY = 24;

Context mContext = this;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getListView().setBackgroundColor(Color.rgb(12, 12, 12));
    getListView().setDividerHeight(0);
    setListAdapter(new ListAdapter(){

        @Override
        public boolean areAllItemsEnabled() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isEnabled(int arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return HOURS_PER_DAY;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getItemViewType(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = getLayoutInflater();
            View listItem = (View) inflater.inflate(R.layout.list_item, getListView(),false);
            TextView hourTV = (TextView) listItem.findViewById(R.id.hourTV);
            TextView amTV = (TextView) listItem.findViewById(R.id.amTV);
            hourTV.setTextColor(Color.BLUE);
            amTV.setTextColor(Color.BLUE);
            final LinearLayout eventsLL = (LinearLayout) listItem.findViewById(R.id.eventsLL);
            hourTV.setText(String.valueOf((position+9)%24));
            //I set am/pm for each entry ... you could specify which entries
            if(((position>=0)&&(position<=2))||((position>=15)&&(position<=23)))
                amTV.setText("am");
            else
                amTV.setText("pm");
            eventsLL.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder alert = new AlertDialog.Builder(mContext); 

                    alert.setTitle("New Event"); 
                    alert.setMessage("Event:"); 

                    // Set an EditText view to get user input 
                    final EditText input = new EditText(mContext); 
                    alert.setView(input); 

                    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, int whichButton) { 
                            TextView A = new TextView(mContext);
                            A.setText(input.getText());
                            A.setTextColor(Color.BLACK);
                            eventsLL.addView(A);
                        } 
                    }); 

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
                        public void onClick(DialogInterface dialog, int whichButton) {
                        } 
                    }); 
                    alert.show();
                }

            });
            return listItem;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 1;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver arg0) {
            // TODO Auto-generated method stub

        }

    });
}

/ drawable/eventbg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <solid Android:color="#FFFFFF"/>
    <corners Android:radius="5px"/>
    <padding Android:left="0dp" Android:top="0dp" Android:right="0dp" Android:bottom="0dp" /> 
</shape>

/ layout/list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:Android="http://schemas.Android.com/apk/res/Android"
  Android:orientation="horizontal"
  Android:layout_width="fill_parent"
  Android:layout_height="fill_parent"
  Android:paddingTop="5dip"
  Android:paddingBottom="5dip"
  Android:background="#CCC">
    <LinearLayout 
        Android:id="@+id/linearLayout1" 
        Android:layout_height="fill_parent" 
        Android:layout_width="wrap_content" 
        Android:orientation="vertical">
        <TextView 
            Android:id="@+id/hourTV" 
            Android:text="" 
            Android:textAppearance="?android:attr/textAppearanceSmall" 
            Android:layout_height="wrap_content" 
            Android:layout_width="wrap_content"/>
        <TextView 
            Android:id="@+id/amTV" 
            Android:text="" 
            Android:textAppearance="?android:attr/textAppearanceSmall" 
            Android:layout_height="wrap_content" 
            Android:layout_width="wrap_content"/>
    </LinearLayout>
    <LinearLayout 
        Android:id="@+id/LLdesign" 
        Android:orientation="horizontal"
        Android:layout_height="fill_parent" 
        Android:layout_width="fill_parent"
        Android:padding="3dip">
        <LinearLayout 
            Android:id="@+id/eventsLL"
            Android:orientation="vertical"
            Android:layout_height="fill_parent" 
            Android:layout_width="fill_parent"
            Android:background="@drawable/eventbg"></LinearLayout>
        </LinearLayout>

</LinearLayout>
26
Sherif elKhatib

lateanswer, um Benutzern zu helfen, die das gleiche Problem haben: Sie können diese Quelle für Ihre implementierende Calendar DayView https://github.com/r3za13/Android-Timeline-Schedule-View verwenden oder anpassen

0
Reza.Abedini

sie können Google Kalender verwenden, um Ihren neuen Kalender anzuzeigen, und hier können Sie Ihre eigenen Ereignisse erstellen. Nachfolgend finden Sie die Klasse zum Erstellen eines neuen Kalenders.

public class CalendarMapper {
    private static final String ACCOUNT_NAME = "private";
    private static final String INT_NAME_PREFIX = "priv";

    @SuppressLint("NewApi")
    private static Uri buildCalUri() {
        return CalendarContract.Calendars.CONTENT_URI
                .buildUpon()
                .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, ACCOUNT_NAME)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
                .build();
    }

    private static ContentValues buildContentValues(Calendar calendar) {
        String dispName = "TUM3";  //Calendar.getName() returns a String
        String intName = INT_NAME_PREFIX + dispName;
        final ContentValues cv = new ContentValues();
        cv.put(Calendars.ACCOUNT_NAME, ACCOUNT_NAME);
        cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        cv.put(Calendars.NAME, intName);
        cv.put(Calendars.CALENDAR_DISPLAY_NAME, dispName);
        //cv.put(Calendars.CALENDAR_COLOR, calendar.getColor());  //Calendar.getColor() returns int
        cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        cv.put(Calendars.OWNER_ACCOUNT, ACCOUNT_NAME);
        cv.put(Calendars.VISIBLE, 1);
        cv.put(Calendars.SYNC_EVENTS, 1);

        return cv;
    }

    @SuppressWarnings("deprecation")
    public static Uri addCalendar(Calendar calendar, ContentResolver cr) {
        if (calendar == null)
            throw new IllegalArgumentException();

        final ContentValues cv = buildContentValues(calendar);
        Uri calUri = buildCalUri();
        Uri cancelUri=cr.insert(calUri, cv);
        return cancelUri;
    }

}

In der Hauptaktivität können Sie Kalender hinzufügen aufrufen, um einen Kalender zu erstellen und ein Ereignis zu diesem Kalender hinzuzufügen.

Uri cancelUri;
 @SuppressLint("NewApi")
    public void displayCal(View view){
           ContentResolver crv = getContentResolver();

           Calendar calendar = Calendar.getInstance();
           cancelUri= CalendarMapper.addCalendar(calendar, crv);

           long calID=2; //ID of the newly created calendar. You can query the calendars table to get the ID of your calendar
           long endMillis = 10100;     
           Calendar beginTime = Calendar.getInstance();
           beginTime.set(2012, 9, 14, 7, 30);
           long startMillis = beginTime.getTimeInMillis();
           Calendar endTime = Calendar.getInstance();
           endTime.set(2012, 9, 14, 8, 45);
           endMillis = endTime.getTimeInMillis();

           ContentResolver cr = getContentResolver();
           ContentValues values2 = new ContentValues();
           values2.put(Events.DTSTART, startMillis);
           values2.put(Events.DTEND, endMillis);
           values2.put(Events.TITLE, "Jazzercise");
           values2.put(Events.DESCRIPTION, "Group workout");
           values2.put(Events.CALENDAR_ID, calID);

           values2.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
           Uri uri = cr.insert(Events.CONTENT_URI, values2);

           Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
           builder.appendPath("time");

           ContentUris.appendId(builder, startMillis);
           Intent intent = new Intent(Intent.ACTION_VIEW)
               .setData(builder.build());
           startActivity(intent);
    }

Um diesen Kalender zu löschen, können Sie dies wie folgt tun

public void delCal(View view){
         ContentResolver crv = getContentResolver();
         crv.delete(cancelUri, null, null);
    }
0
Haris Iltifat