2015-10-05 94 views
0

我正在開發一個需要在Onclick方法上打開ShareAction菜單的項目,我似乎無法讓它工作。首先,我有ShareAction菜單工作,但只在設置菜單中,這是;是我,當我試圖將其應用到的ImageButton的onclick方法Android我怎樣才能讓ShareActionProvider工作在ImageButton上點擊

// This is inside the Oncreate Method 

share= (ImageButton)findViewById(R.id.share); 
     share.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ShareActionProvider nn= new ShareActionProvider(Login.this); 
       Intent ShareIntent=new Intent(Intent.ACTION_SEND); 
       ShareIntent.setAction(Intent.ACTION_SEND); 
       ShareIntent.setType("text/plain"); 
       ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share"); 
       nn.setShareIntent(ShareIntent); 
      } 
     }); 

上面的代碼不起作用它不給任何錯誤或沒有。我有相同的代碼使用設置菜單的工作,做了以下 菜單的活動

<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=".Login"> 

    <item android:id="@+id/action_settings" android:title="@string/action_settings" 
     android:orderInCategory="200" android:icon="@drawable/overflow" app:showAsAction="never" /> 
    <item android:id="@+id/action_share" android:title="@string/action_share" android:orderInCategory="100" app:showAsAction="ifRoom" 
     android:actionProviderClass="android.widget.ShareActionProvider" 
     /> 

</menu> 

然後簡單地把它添加到菜單

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_login, menu); 
    MenuItem shareItem= menu.findItem(R.id.action_share); 

    ShareActionProvider mShare= (ShareActionProvider)shareItem.getActionProvider(); 
    Intent ShareIntent=new Intent(Intent.ACTION_SEND); 
    ShareIntent.setAction(Intent.ACTION_SEND); 
    ShareIntent.setType("text/plain"); 
    ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share"); 
    mShare.setShareIntent(ShareIntent); 
    return true; 
} 

我缺少什麼或者做錯了是不是讓該菜單在Oncreate中打開?

回答

1

我的工作,需要的是ShareAction菜單上的一個onclick方法打開,我似乎無法得到它的工作

這沒有多大意義的項目。 ShareActionProvider是一個操作欄項目。它有自己的「按鈕」,允許用戶共享內容,基於配置的Intent指示要完成什麼樣的共享(例如,MIME類型)。

因此,有三種可能性在這裏,我可以看到:

  1. 只需使用ShareActionProvider,遠離ImageButton的。

  2. 擺脫ShareActionProvider,並採取其他措施來顯示ImageButtononClick()中的共享操作列表。例如,您可以將Intent包裝在Intent.createChooser()中,並在Intent中調用startActivity(),從onClick() - 如果有兩個或更多與Intent匹配的活動,用戶將獲得選擇器「菜單」。

  3. ShareActionProvider把它暴露某種open()方法,它允許您從ImageButtononClick()激活它。我無法想象這種情況是否合理(爲什麼點擊這裏的按鈕會彈出一個下拉菜單?)。

+0

噢好吧完美謝謝讓我知道,我是新來的。由於我需要用戶共享單個帖子,因此我將在第二個選項上工作。 –

相關問題