按钮长按上的上下文菜单


如何

使我的应用程序在长项单击时显示上下文菜单?我已经编写了这段代码,它正在工作。我怎样才能让它在我点击某个按钮时显示出来?

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textColor="#04B404" />

法典:

TextView tv=(TextView)findViewById(R.id.tv);
tv.setOnCreateContextMenuListener(this);

@Override 
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuinfo) {
    super.onCreateContextMenu(menu, view, menuinfo);
    menu.setHeaderTitle("Set as");
    menu.add(menu.FIRST, Menu.NONE, 0, "Set as Wallpaper");
    menu.add(menu.FIRST+1, Menu.NONE, 0, "Download");
    menu.add(menu.FIRST+2, Menu.NONE, 0, "Info);
}

试试这段代码:

public class MainActivity extends ListActivity {  
  private String[] items;
  private List<String> list;
  private  ArrayAdapter<String> adapter;
  private int position;
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.activity_main);  
       fillData();  
       registerForContextMenu(getListView());
  }  
  private void fillData() {  
       items = new String[] {"Monday", "Tuesday", "Wednesday",   
                        "Thursday", "Friday", "Saturday", "Sunday"};
       list = new ArrayList<String>();
       Collections.addAll(list, items);
       adapter = new ArrayAdapter<String>(this, R.layout.row,
       R.id.r_text, list);  
       setListAdapter(adapter);            
  }  
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater m = getMenuInflater();
    m.inflate(R.menu.our_context_menu, menu);
  }
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.delete_item:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            position = (int) info.id;
            list.remove(position);
            this.adapter.notifyDataSetChanged();
            return true;
  }
  return super.onContextItemSelected(item);
}

}

活动.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical" >
        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
</LinearLayout> 

行.xml:

<?xml version="1.0" encoding="utf-8"?>  
<TextView   
      xmlns:android="http://schemas.android.com/apk/res/android"  
      android:textIsSelectable="true"  
      android:id="@+id/r_text"   
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent"  
      android:padding="10dip"  
      android:textColor="#ffffff"  
      android:textSize="17sp"/>  

现在在res中创建一个menu文件夹并将此文件放入其中。

上下文菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
      android:id="@+id/delete_item"
      android:title="@string/delete_string"/>    
</menu>

您可以使用 registerForContextMenu(View v) 方法,该方法采用要在其上注册 ContextMenu 的视图。

因此,如果您的按钮称为myButton,

 registerForContextMenu(myButton);

因此,如果您希望长按,只需添加LongClickListener并使用上述内容注册您的按钮即可。

下面是一个例子。

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:visibility="gone"
        android:textColor="#04B404" />

现在拿一个按钮,你所做的所有wd文本视图都做同样的事情,点击那个按钮就像这样做一样

Button dialogButtonCancel = (Button) findViewById(R.id.dialogButtonCancel);
                    dialogButtonCancel.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        textview.setVisibility(View.VISIBLE);                              

                        }
                    });

XML 代码

<Button
            android:id="@+id/btnFb"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1"
            android:text="Button" />

爪哇代码

btnFb.setOnClickListener(FbListenser);
private OnClickListener FbListenser = new OnClickListener() {
    public void onClick(View v) {
                    PopupMenu popupMenu = new PopupMenu(activity.getApplicationContext(), v);
                        List<String> lstFb = new List<String>();
                        for (int i = 0; i < lstFb.size(); i++) {
                                popupMenu.getMenu().add(lstFb.get(i));
                        }
                        popupMenu.setOnMenuItemClickListener(new                         PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                                    String Value = item.toString();
                         }
                });
        popupMenu.show();
        }
};

调用registerForContextMenu(tv);并向其传递要附加上下文菜单的视图。

相关内容

  • 没有找到相关文章

最新更新