我正在寫一個應用程序,它管理日曆應用程序中的鬧鐘和事件。當我嘗試獲取有關在自定義對話框中報告的事件的所有信息時,我遇到了一個問題。我嘗試了很多東西。我在互聯網上看到的最後一件事使用意圖。然而,儘管Dialog可以發送信息,但我不知道如何接收它。我嘗試用廣播recevier的東西,但它沒有工作......從CustomDialog傳遞信息到主活動
- >下面是對話的代碼:
public class AddingEventMenu extends Dialog implements OnClickListener {
Event event = new Event();
private Button b;
Context context;
public AddingEventMenu(Context context) {
super(context);
this.context = context;
this.setContentView(R.layout.addbuttonlayout);
this.setTitle("Adding Event");
this.setCancelable(true);
this.b = (Button) findViewById(R.id.addEventButton);
this.b.setOnClickListener(this);
}
public void onClick(View v) {
String color;
DatePicker DP = (DatePicker) findViewById(R.id.datePicker);
TimePicker TP = (TimePicker) findViewById(R.id.timePicker);
EditText ET = (EditText) findViewById(R.id.eventText);
Spinner SC = (Spinner) findViewById(R.id.colorSelector);
Intent intent = new Intent();
intent.setAction("New Event");
color = SC.getSelectedItem().toString();
if (color == "Red") event.eventColor = Color.RED;
if (color == "Green") event.eventColor = Color.GREEN;
if (color == "Blue") event.eventColor = Color.BLUE;
if (color == "Magenta") event.eventColor = Color.MAGENTA;
intent.putExtra("Text", ET.getText().toString());
intent.putExtra("Day", DP.getDayOfMonth());
intent.putExtra("Month", DP.getMonth());
intent.putExtra("Year", DP.getYear());
intent.putExtra("Hour", TP.getCurrentHour() * 100 + TP.getCurrentMinute());
intent.putExtra("Color", color);
context.sendBroadcast(intent);
Log.d("State", "Es un primer paso");
this.dismiss();
}
- >這裏的主菜單代碼:
public class Timetable extends Activity {
public static List<Event> eventList;
EventManager eventManager;
LinearLayout eventListLayout;
ImageView addButton;
EventDatabaseManager db;
BroadcastReceiver BR;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_timetable);
BR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == "New event"){
Bundle extras = new Bundle();
Event event = new Event();
extras = intent.getExtras();
event.eventText = extras.getString("Text");
event.eventDay = extras.getInt("Day");
event.eventMonth = extras.getInt("Month");
event.eventYear = extras.getInt("Year");
event.eventHour = extras.getInt("Hour");
event.eventColor = extras.getInt("Color");
Log.d("State", "Esta funcionando");
saveNewEvent(event);
}
}
};
db = new EventDatabaseManager(this);
eventListLayout = (LinearLayout) findViewById(R.id.eventListLayout);
eventManager = new EventManager(this);
eventList = eventManager.getEvents();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_timetable, menu);
return true;
}
public void onAddButton(View button) {
AddingEventMenu aEM = new AddingEventMenu(this);
aEM.show();
}
public void saveNewEvent(Event event) {
eventList.add(event);
db.addEvent(event);
}
在主要活動中,我定義了broadcastreceiver,但我認爲這是錯誤的,因爲它沒有任何東西。
我希望有人能幫助我。謝謝你的時間。
哦,很好的解決方案!我將從DialogFragment中調查更多。謝謝 :) – Bardo91