如何根据Android中用户选择的数据创建通知

时间:2015-10-14 10:20:30

标签: android

我在如何使这个功能上遇到问题。我现在为这个布局测试了很多代码,但它不会工作。这是我制作的布局:

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout2"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="60dp"
        android:padding="10dp"
        android:background="#f7a29d">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Goal Name:"
            android:id="@+id/tt"
            android:layout_alignBottom="@+id/linearLayout2"
            android:layout_alignParentStart="true"
            android:textColor="#000000"
            android:paddingBottom="5dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/goal_name"
            android:textColor="#000000"
            android:padding="10dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Start Date:"
            android:id="@+id/textView24"
            android:textColor="#000000"
            android:paddingTop="5dp"
            android:paddingBottom="5dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/sdate"
            android:textColor="#000000"
            android:padding="10dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="End Date:"
            android:id="@+id/tt2"
            android:textColor="#000000"
            android:paddingTop="5dp"
            android:paddingBottom="5dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/edate"
            android:textColor="#000000"
            android:padding="10dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="NOTIFICATIONS OPTIONS"
            android:id="@+id/textView25"
            android:textColor="#000000"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Time:"
            android:id="@+id/textView35"
            android:layout_row="2"
            android:layout_column="0"
            android:textColor="#000000" />

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_row="19"
            android:layout_column="0">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="ShowTime"
                android:id="@+id/showtime"
                android:layout_row="1"
                android:layout_column="0"
                android:textColor="#000000"
                android:layout_marginLeft="20dp" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Set Time"
                android:id="@+id/settime"
                android:layout_row="1"
                android:layout_column="1"
                android:layout_marginLeft="5dp" />

            <TimePicker
                android:id="@+id/timePicker"
                android:layout_width="1dp"
                android:layout_height="1dp"
                android:visibility="invisible"/>

        </GridLayout>

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Repeat:"
                android:id="@+id/textView26"
                android:layout_below="@+id/linearLayout2"
                android:layout_row="0"
                android:layout_column="0"
                android:textColor="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="ShowRepeat"
                android:id="@+id/showRepeat"
                android:layout_row="2"
                android:layout_column="0"
                android:textColor="#000000"
                android:background="#ffffff"
                android:padding="8dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Repetition Option:"
                android:id="@+id/textView28"
                android:layout_row="3"
                android:layout_column="0"
                android:textColor="#000000" />

            <ToggleButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New ToggleButton"
                android:id="@+id/toggleButton"
                android:layout_row="2"
                android:layout_column="1"
                android:textColor="#000000"
                android:background="#9bd8ea" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="set"
                android:id="@+id/setRepetition"
                android:layout_row="4"
                android:layout_column="0"
                android:textColor="#000000"
                android:background="#3a738a" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Sound Option:"
                android:id="@+id/textView30"
                android:layout_row="7"
                android:layout_column="0"
                android:textColor="#000000" />

            <ToggleButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New ToggleButton"
                android:id="@+id/toggleButton2"
                android:layout_row="8"
                android:layout_column="0"
                android:textColor="#000000"
                android:background="#9bd8ea" />

        </GridLayout>

    </LinearLayout>

看起来像这样:

目标名称:(已提供数据)

开始日期:(已提供数据)

结束日期:(已提供数据)

通知选项

时间: showTime [设置] - &gt;时间选择器弹出

重复: showRepeat [切换按钮] - &gt;选择是否重复一次通知

重复选项: [设置] - &gt;弹出选项和选项的警报对话框是在showRepeat TextView

中打印的

声音: [切换按钮] - &gt;选择通过声音通知

SetNotifyActivity.java

package com.citu.goalsettingorganizer;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.app.Dialog;
import android.app.Activity;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;


import java.util.Calendar;



public class SetNotifyActivity extends AppCompatActivity {
    private TextView goalname,startdate,enddate,showrepeat;
    private TextView showTime;
    private TimePicker timePicker;
    private Button setTime,setRepeat;

    private int hour;
    private int minute;
    private String format;

    static final int TIME_PICKER_DIALOG_ID = 100;

    MyDBAdapter dbhandler;
    public int goal_id;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_notify);

        Bundle extras = getIntent().getExtras();

        if (extras == null) {
            return;
        }

        goalname = (TextView)findViewById(R.id.goal_name);
        startdate = (TextView)findViewById(R.id.sdate);
        enddate = (TextView)findViewById(R.id.edate);
        showrepeat = (TextView)findViewById(R.id.showRepeat);

        showTime = (TextView) findViewById(R.id.showtime);
        setTime = (Button)findViewById(R.id.settime);
        timePicker = (TimePicker)findViewById(R.id.timePicker);

        setRepeat = (Button)findViewById(R.id.setRepetition);

        goal_id = Integer.parseInt(extras.getString("goalid"));

        //Create database
        dbhandler = new MyDBAdapter(this);

        displayDetails();
        displayTime();
        changeTimeButton();
        selectRepeat();

    }

    public void displayDetails(){

        List<Goals> oneGoal = dbhandler.getLatestGoal(goal_id);

        for (final Goals goals : oneGoal) {
            goalname.setText(goals.getGoalName());
            startdate.setText(goals.getSMonth() + "/" + goals.getSDay() + "/" + goals.getSYear());
            enddate.setText(goals.getEMonth() + "/" + goals.getEDay() + "/" + goals.getEYear());

        }

    }

    public void displayTime(){

        final Calendar getTime = Calendar.getInstance();
        hour = getTime.get(Calendar.HOUR_OF_DAY);
        minute = getTime.get(Calendar.MINUTE);

        // set current time into textview
        showTime(hour,minute);

        // set current time into timepicker
        timePicker.setCurrentHour(hour);
        timePicker.setCurrentMinute(minute);


    }

    public void changeTimeButton(){

        setTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showDialog(TIME_PICKER_DIALOG_ID);

            }
        });
    }

    public void showTime(int hour, int min) {
        if (hour == 0) {
            hour += 12;
            format = "AM";
        }
        else if (hour == 12) {
            format = "PM";
        } else if (hour > 12) {
            hour -= 12;
            format = "PM";
        } else {
            format = "AM";
        }
        showTime.setText(new StringBuilder().append(pad(hour)).append(" : ").append(pad(min))
                .append(" ").append(format));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    private TimePickerDialog.OnTimeSetListener timePickerListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int selectedHour,
                                      int selectedMinute) {
                    hour = selectedHour;
                    minute = selectedMinute;

                    // set current time into textview
                    showTime(hour,minute);

                    // set current time into timepicker
                    timePicker.setCurrentHour(hour);
                    timePicker.setCurrentMinute(minute);

                }
            };

    // On clicking repeat type button
    public void selectRepeat(){
        setRepeat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder alert = new AlertDialog.Builder(SetNotifyActivity.this);

                final EditText number = new EditText(SetNotifyActivity.this);
                alert.setMessage("Enter a Number:");
                alert.setTitle("");


                alert.setView(number);

                alert.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        final String tempnumber = number.getText().toString();

                        final CharSequence[] items = {"Hours","Days", "Weeks", "Months"};

                        AlertDialog.Builder builder = new AlertDialog.Builder(SetNotifyActivity.this);
                        builder.setTitle("Select Period:");
                        builder.setItems(items, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {

                                showrepeat.setText(tempnumber+" "+items[item]);

                                dialog.dismiss();

                            }
                        }).show();

                    }
                });

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // what ever you want to do with No option.
                    }
                });

                alert.create().show();
            }
        });


    }





    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case TIME_PICKER_DIALOG_ID:
                // set time picker as current time
                return new TimePickerDialog(this,
                        timePickerListener, hour, minute,false);

        }
        return null;
    }



    public void done(){
        Intent myIntent = new Intent(SetNotifyActivity.this, MainMenuActivity.class);
         startActivity(myIntent);

    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_set_notify, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_done) {
            done();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我的问题是当用户完成设置通知时,我无法使其正常工作。通知将根据他/她的选择而有效。我似乎无法找到任何我能够轻易理解的来源。

  • 我删除了我测试的代码,以使通知工作,使它看起来不凌乱。它无论如何都不起作用 -

0 个答案:

没有答案