gpt4 book ai didi

android - onLongClick 显示菜单选项

转载 作者:搜寻专家 更新时间:2023-11-01 09:07:22 26 4
gpt4 key购买 nike

这是我第一次来这里,我一直在寻找这个问题的答案 2 天,但没有任何效果。这是,我想在用户按住所选项目并长按时显示 MenuOptions(编辑,删除)。我的代码:

public class ProjetoProTelefoneActivity extends ListActivity {
public final static String ID_EXTRA = "br.com.DaniloDeLuca.ProjetoProTelefone._ID";
Cursor modelo = null;
RestaurantAdapter adapter = null;
RestauranteHelper helper=null;
SharedPreferences prefs=null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

helper = new RestauranteHelper(this);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
initList();
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}

public void onDestroy(){
super.onDestroy();

helper.close();
}
public void onListItemClick(ListView list, View view,
int position,long id){
Intent i=new Intent(ProjetoProTelefoneActivity.this, DetailForm.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}

public void onListItemLongClic( View view,int position,Menu menu){
new MenuInflater(this).inflate(R.menu.option,menu);
super.onCreateOptionsMenu(menu);

}

//hook into menu button for activity
public boolean onCreateOptionsMenu(Menu menu){
new MenuInflater(this).inflate(R.menu.option,menu);
return(super.onCreateOptionsMenu(menu));
}
/// when menu button option selected
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.add){
startActivity(new Intent(ProjetoProTelefoneActivity.this, DetailForm.class));
return(true);
}
else if(item.getItemId()==R.id.prefs){

startActivity(new Intent(this, EditPreferences.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
private SharedPreferences.OnSharedPreferenceChangeListener prefListener=
new SharedPreferences.OnSharedPreferenceChangeListener() {

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if(key.equals("sort_order")){
initList();
}

}
};
private void initList(){
if(modelo!=null){
stopManagingCursor(modelo);
modelo.close();
}
modelo =helper.getAll(prefs.getString("sort_order","nome DESC"));
startManagingCursor(modelo);

adapter = new RestaurantAdapter(modelo);
setListAdapter(adapter);

}

class RestaurantAdapter extends CursorAdapter {
RestaurantAdapter(Cursor c) {
super(ProjetoProTelefoneActivity.this, c);
}

public void bindView(View row, Context ctxt,
Cursor c) {
RestaurantHolder holder=(RestaurantHolder)row.getTag();

holder.populateFrom(c, helper);
}


public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
RestaurantHolder holder=new RestaurantHolder(row);

row.setTag(holder);

return(row);
}
}

static class RestaurantHolder {
private TextView name=null;
private TextView address=null;
private ImageView icon=null;

RestaurantHolder(View row) {
name=(TextView)row.findViewById(R.id.title);
address=(TextView)row.findViewById(R.id.address);
icon=(ImageView)row.findViewById(R.id.icon);
}

void populateFrom(Cursor r,RestauranteHelper helper) {
name.setText(helper.getNome(r));
address.setText(helper.getEnd(r));


if (helper.getTipo(r).equals("casa")) {
icon.setImageResource(R.drawable.casa_icon);
}
else if (helper.getTipo(r).equals("apartamento")) {
icon.setImageResource(R.drawable.apartamento_icon);
}
else {
icon.setImageResource(R.drawable.comercio_ico);
}
}
}

}

仅当我选择 MenuButton 时才有效。但我想让“MenuButton” Activity 成为长按操作,我不知道是否清楚。

最佳答案

您想要的是 registerForContextMenu,即针对 Activity 注册您的 ListView,以便在长按它时出现一个新菜单将被创建并显示给用户。见

http://developer.android.com/reference/android/app/Activity.html#registerForContextMenu%28android.view.View%29

关于android - onLongClick 显示菜单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com