首页 > 解决方案 > 获取用户输入到 NotificationCompiler

问题描述

我的问题是我想从我的 Fragment.xmlandroid:id="@+id/eingUserAlarm"中的 Edittext 获取用户输入以显示在显示通知中。在我正在使用的 NotificationHelper.java 中,
titelAlarm = getResources(R.id.eingUserAlarm).toString(); String nameAlarm = titelAlarm.getText().toString();
但它无法解析findViewById。我怎样才能膨胀布局?当我尝试时它显示错误。FragmentTransaction 也不起作用,因为我不会写beginTransaction()
我的AlarmTimePicker.java

public class AlarmTimePicker extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getParentFragment() , hour, minute, DateFormat.is24HourFormat(getActivity()));
     }
}

NotificationHelper.java:(这里发生错误)

public class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelName = "Channel Name";
    private NotificationManager mManager;
    private EditText titelAlarm;

    public NotificationHelper(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);

        getManager().createNotificationChannel(channel);
    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }

        return mManager;
    }

    public NotificationCompat.Builder getChannelNotification() {
        titelAlarm = findViewById(R.id.eingUserAlarm);
        String nameAlarm = titelAlarm.getText().toString();
        return new NotificationCompat.Builder(getApplicationContext(), channelID)
                .setContentTitle("Erinnerung")
                .setContentText(nameAlarm)
                .setSmallIcon(R.drawable.ic_one);
    }
}

我的按钮:

    <Button
    android:id="@+id/button_timepicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Zeit stellen" />

警报接收器.java:

public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationHelper notificationHelper = new NotificationHelper(context);
    NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
    notificationHelper.getManager().notify(1, nb.build());
}

}

警报片段.java

        final View fraAla = inflater.inflate(R.layout.fragment_alarm, container, false);

    edText = fraAla.findViewById(R.id.eingUserAlarm);
    mTextView = fraAla.findViewById(R.id.textView);

    Button buttonTimePicker = fraAla.findViewById(R.id.button_timepicker);
    buttonTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment timePicker = new AlarmTimePicker();
            timePicker.show(getChildFragmentManager(), "time picker");
        }
    });

    Button buttonCancelAlarm = fraAla.findViewById(R.id.button_cancel);
    buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
    return fraAla;
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);

    updateTimeText(c);
    startAlarm(c);
}

private void updateTimeText(Calendar c) {
    String timeText = "Wir erinnern dich um ";
    timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());

    mTextView.setText(timeText);
}

private void startAlarm(Calendar c) {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE, 1);
    }

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}

private void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    alarmManager.cancel(pendingIntent);
    mTextView.setText("Wir werden nicht stören!");
}

}

感谢所有试图提供帮助的人!

标签: androidinputnotifications

解决方案


推荐阅读