2014-02-27 92 views
2

如何讓我的應用程序在長條件單擊上顯示上下文菜單? 我寫了這段代碼,它正在工作。 我怎樣才能讓它顯示當我點擊某個按鈕?按鈕長按點擊上下文菜單?

<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); 

} 
+0

使用上下文操作欄更清潔。 http://developer.android.com/design/patterns/actionbar.html#contextual –

回答

3

試試這個代碼:

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); 
} 

}

Activity.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> 

row.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> 
+0

令人印象深刻的答案。更清晰 – 2014-02-27 13:20:09

+0

這就是我談論的內容。 +1上 –

0
<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);        


         } 
        }); 
+0

嗯,我試過你的,它崩潰的應用程序:http://prntscr.com/2wa92w – user3094736

+0

老兄你有文本視圖和所有ids你哪早先採取寫所有你以前的代碼,然後添加此按鈕花花公子 –

+0

我認爲你沒有采取文本視圖編號之前,你寫按鈕代碼 –

0

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" /> 

Java代碼的

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(); 
     } 
}; 
0

呼叫registerForContextMenu(tv);並把它傳遞要附加上下文菜單到視圖。

+0

我試着用你的代碼,根本不顯示上下文菜單。 – user3094736

1

您可以使用registerForContextMenu(查看V)方法,它發生在你想註冊你的ContextMenu觀。

所以,如果你的按鈕被稱爲myButton的,

registerForContextMenu(myButton); 

所以,如果你想擁有它長按只需添加onLongClickListener和使用上述註冊按鈕。

繼承人和example.