这是我的菜单场合布局…我需要在某些情况下显示此删除图标…
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="ringee.app.com.ringeeapp.Occasion">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/menu_cancel_event"
android:icon="@drawable/delete_icon"
android:title="Cancel event"
app:showAsAction="ifRoom"/>
</menu>
是否可以在此处更改此删除图标的可见性…请帮助我查找代码
这是我的场合活动代码….这里是添加事件和更新事件时显示的删除图标..这里我需要仅当事件更新时才显示的删除图标..我不需要在添加事件时显示该图标….
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
this.menu=menu;
getMenuInflater().inflate(R.menu.menu_occasion, menu);
hide();
show();
return true;
}
public boolean show()
{
menu.findItem(R.id.menu_cancel_event).setVisible(true);
return true;
}
public boolean hide(){
menu.findItem(R.id.menu_cancel_event).setVisible(false);
return false;
}
// delete the selected event from event list added here
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_cancel_event:
//Setting the value for deleted event
eventMO.setIsDelete(1);
del();
//Triggering the Create event
doOnCLickAndOnOptionSelected();
return true;
}
return super.onOptionsItemSelected(item);
}
private void del() {
//Deleting the event from SQLLite
SQLiteDatabase db = dbHelper.getWritableDatabase();
final long Id = eventMO.getEventId();
db.delete("event", "Event_ID" + " = ?", new String[]{String.valueOf(Id)});
db.close();
}
// Common for delete event and save event(Create button click listener)
private void doOnCLickAndOnOptionSelected() {
//We need create button click listener for both delete as well as save the event.so we have this common method.we call this method from delete event and save event
prgDialog = new ProgressDialog(OccasionActivity.this);
// Set Progress Dialog Text
prgDialog.setMessage("Please wait...");
// Set Cancelable as False
prgDialog.setCancelable(false);
prgDialog.show();
UserMO userMO = dbHelper.getRingeeUserData(1);
eventMO.setText(custom_Text.getText().toString());
eventMO.setPlace(event_Place.getText().toString());
eventMO.setEndTime(end_Time);
eventMO.setStartTime(start_Time);
try {
//date and time format changed here
String eventDate = btn_Date.getText().toString();
DateFormat date = new SimpleDateFormat("dd-M-yyyy");
Date date1 = date.parse(eventDate);
DateFormat convertDate = new SimpleDateFormat(" yyyy-MM-dd hh:mm:ss");
eventDate = convertDate.format(date1);
eventMO.setEventDate(eventDate);
} catch (ParseException e) {
e.printStackTrace();
}
//eventMO.setEventDate(btn_Date.getText().toString());
eventMO.setRingeeUserId(userMO.getRingeeUserId());
//event update functionality added here
final Long hidden_Text2 = eventId2;
if ((eventMO.getText() != null) && (eventMO.getPlace() != null) && (eventMO.getEventDate() != null)) {
if (hidden_Text2 != null) {
eventMO.setEventId(hidden_Text2);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
return eventDelegates.updateEvent(eventMO, context);
}
@Override
protected void onPostExecute(String arg0) {
prgDialog.dismiss();
Intent contactAct = new Intent(getApplicationContext(), UserDashBoardActivity.class);
// Clears History of Activity
contactAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(contactAct);
}
}.execute(null, null, null);
Toast.makeText(getApplicationContext(), "Details updated successfully", Toast.LENGTH_LONG).show();
} else {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
return eventDelegates.addEvent(eventMO, context);
}
@Override
protected void onPostExecute(String eventId) {
prgDialog.dismiss();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("eventId", eventId);
editor.commit();
Intent contactAct = new Intent(getApplicationContext(), ContactActivity.class);
// Clears History of Activity
contactAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(contactAct);
}
}.execute(null, null, null);
Toast.makeText(getApplicationContext(), "Details added successfully", Toast.LENGTH_LONG).show();
}
} else {
prgDialog.dismiss();
Toast.makeText(context, "Please check Event custom text or Event place or Event date field", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_occasion);
context = getApplicationContext();
seekBar_startTime = (SeekBar) findViewById(R.id.seekBar1);
seekBar_startTime.setProgress(0);
seekBar_startTime.setMax(1440); //minutes in 24 hours(24*60)
seekBar_startTime.setProgress(1);
textView_startTime = (TextView) findViewById(R.id.textView1);
seekBar_endTime = (SeekBar) findViewById(R.id.seekBar2);
seekBar_endTime.setProgress(0);
seekBar_endTime.setMax(1440); //minutes in 24 hours(24*60)
seekBar_endTime.setProgress(1);
textView_endTime = (TextView) findViewById(R.id.textView2);
btn_Date = (Button) findViewById(R.id.event_date_button);
event_Place = (TextView) findViewById(R.id.Enter_Place);
custom_Text = (TextView) findViewById(R.id.Custom_Text);
checkBox_onEventDay=(CheckBox) findViewById(R.id.cb_onEventDay);
checkBox_2DaysBefore=(CheckBox) findViewById(R.id.cb_2DaysBefore);
checkBox_aWeekBefore=(CheckBox) findViewById(R.id.cb_aWeekBefore);
btn_create_button = (Button) findViewById(R.id.create_button);
sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
dbHelper = new DatabaseHelper(context);
btn_Date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "Occasion Date");
}
});
seekBar_startTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
int minutes;
int hours;
minutes =progressValue % 60;
hours = progressValue/60;
int Hours=hours;
String am_pm;
if (hours==24)
{
hours = hours-12;
am_pm="AM";
}
else if
(hours> 12)
{
hours= hours- 12;
am_pm = "PM";
}
else if (hours ==0) {
hours =hours+12;
am_pm = "AM";
}
else if (hours <12) {
am_pm = "AM";
}
else
{
am_pm="PM";
}
if(minutes < 10)
{
//It shows 12 hours format with AM/PM to user
start_Time = String.valueOf(" "+hours+":0"+minutes+am_pm);
textView_startTime.setText("Event Start Time :" + start_Time);
//It pass 24hours format to database
start_Time = String.valueOf(Hours+":0"+minutes);
}
else {
//It shows 12 hours format with AM/PM to user
start_Time = String.valueOf(" "+hours+":"+minutes+am_pm);
textView_startTime.setText("Event Start Time :" + start_Time);
//It pass 24hours format to database
start_Time = String.valueOf(Hours+":"+minutes);
}
}
});
seekBar_endTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue,boolean fromUser) {
int minutes;
int hours;
minutes = progressValue % 60;
hours = progressValue/60;
int Hours=hours;
String am_pm;
if (hours==24)
{
hours = hours-12;
am_pm="AM";
}
else if(hours> 12)
{
hours= hours- 12;
am_pm = "PM";
}
else if (hours ==0) {
hours =hours+12;
am_pm = "AM";
}
else if (hours <12) {
am_pm = "AM";
}
else
{
am_pm="PM";
}
if(minutes < 10)
{
//It shows 12 hours format with AM/PM to user
end_Time = String.valueOf(""+hours+":0"+minutes+am_pm);
textView_endTime.setText("Event Start Time :" + end_Time);
//It pass 24hours format to database
end_Time = String.valueOf(Hours+":0"+minutes);
}
else {
//It shows 12 hours format with AM/PM to user
end_Time = String.valueOf(""+hours+":"+minutes+am_pm);
textView_endTime.setText("Event Start Time :" + end_Time);
//It pass 24hours format to database
end_Time = String.valueOf(Hours+":"+minutes);
}
}
});
//eventid get from OccasionFragment here
Bundle bundle = this.getIntent().getExtras();
if (bundle != null) {
Long eventId1 = bundle.getLong("EventID");
eventId2 = eventId1;
String eventName = bundle.getString("EventName");
custom_Text.setText(eventName);
String eventPlace = bundle.getString("EventPlace");
event_Place.setText(eventPlace);
String eventDate = bundle.getString("EventDate");
try {
//String eventDate = bundle.getString("EventDate");
//time string removed from date here
String result = eventDate.split(" ")[0];
DateFormat sourceDate = new SimpleDateFormat("yyyy-MM-dd");
// parse the date string into Date object
Date date = sourceDate.parse(result);
DateFormat convertDate = new SimpleDateFormat("dd-M-yyyy");
// format the date into another format
result = convertDate.format(date);
btn_Date.setText(result);
} catch (ParseException e) {
e.printStackTrace();
}
//Event Start Time and Event End Time string added before the time here
String eventStart = bundle.getString("EventStart");
textView_startTime.setText("Event Start Time :" + eventStart);
String eventEnd = bundle.getString("EventEnd");
textView_endTime.setText("Event End Time :" + eventEnd);
}
checkBox_onEventDay.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(OccasionActivity.this,
"Checked", Toast.LENGTH_LONG).show();
}}
});
checkBox_2DaysBefore.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(OccasionActivity.this,
"Checked", Toast.LENGTH_LONG).show();
}}
});
checkBox_aWeekBefore.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(OccasionActivity.this,
"Checked", Toast.LENGTH_LONG).show();
}}
});
btn_create_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doOnCLickAndOnOptionSelected();
}
});
}
@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Date = new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append(" ").toString();
btn_Date.setText(new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" ").toString());
}
}
}
在这里,我只需要在更新详细信息时显示删除图标…..我需要在创建事件时隐藏该图标…创建事件代码在“创建时受保护的void”中..我需要添加此隐藏方法…我在该创建方法中尝试了此操作,但它显示了一个错误…请帮助我
最佳答案
试试下面的代码,
public class MainActivity extends Activity {
Menu menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_act_app_home_new, menu);
return true;
}
void show(){
menu.findItem(R.id.menu_cancel_event).setVisible(true);
}
void hide(){
menu.findItem(R.id.menu_cancel_event).setVisible(false);
}
}
如果这对你有用请告诉我…
并将其标记为答案,以便对其他人有用