这是我的菜单场合布局......我需要在某种情况下显示此删除图标....
<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>
是否可以在此处更改此delete_icon的可见性...请帮我查找代码
这是我的场合活动代码....这里删除添加事件时显示的图标以及更新事件。我只有在事件更新时才需要显示删除图标...我不需要在添加事件时显示...
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());
}
}
}
这里我只需要在更新细节时显示删除图标.....我需要在创建事件时隐藏该图标...创建事件代码在&#34;受保护无效在创建&# 34; ....我需要添加这个隐藏方法...我在这个创建方法中尝试了这个但它显示错误...请帮帮我
答案 0 :(得分:1)
尝试以下代码,
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);
}
}
请告诉我这是否适合您......
并将其标记为答案,以便对其他人有用
答案 1 :(得分:0)
MenuItem不是您布局的常规视图。它的特殊之处,完全不同。您的代码为item返回null,导致崩溃。你需要做的是:
MenuItem item = menu.findItem(R.id.addAction);
然后获取指向此项目的 MenuItem ,在其上调用 setVisible()以调整其可见性,然后调用
invalidateOptionsMenu()
您的活动,因此ActionBar菜单会相应调整。
答案 2 :(得分:0)
只需创建菜单:
boolean eventUpdated = true/false;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_occasion, menu);
return true;
}
并根据您的条件设置可见性:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(R.id.menu_cancel_event).setVisible(eventUpdated);
return true;
}
每次条件更改为刷新菜单时调用invalidateOptionsMenu()
。