2013-12-02 2 views
0

Я использую shareactionprovider для совместного использования текста, но не могу заставить его работать. Тот же код отлично работает с опцией меню. Когда я обмениваюсь текстом, используя shareactionprovider, текст не используется, но когда я обмениваюсь одним и тем же текстом, используя общий доступ к меню, текст становится общим.код не работает с shareactionprovider, но тот же код работает с опцией меню

жаль мой бедный английский

public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.share2, menu); 
     menu.add(Menu.NONE, MENU_ITEM1, Menu.NONE, "Share"); 
     MenuItem mShareActionProviderItem = (MenuItem) menu.findItem(R.id.menu_share2); 

     mShareActionProvider = (ShareActionProvider) mShareActionProviderItem.getActionProvider(); 

     Intent t = new Intent(Intent.ACTION_SEND); 
     t.setAction(Intent.ACTION_SEND); 
     t.setType("text/plain"); 
     CharSequence displayContents = contentsTextView.getText().toString(); 
     t.putExtra(Intent.EXTRA_TEXT,displayContents); 
     mShareActionProvider.setShareIntent(t); 
     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 



     case MENU_ITEM1: 

       Intent shareIntent2 = new Intent(); 
       shareIntent2.setAction(Intent.ACTION_SEND); 
       shareIntent2.setType("text/plain"); 
       CharSequence displayContents2 = contentsTextView.getText().toString(); 
       shareIntent2.putExtra(Intent.EXTRA_TEXT,displayContents2); 
       startActivity(shareIntent2); 
       break; 




      } 
      return super.onOptionsItemSelected(item); 
    } 
+0

вы должны тикать ответ здесь. – Fattie

ответ

1

contentsTextView, вероятно, пуст, когда onCreateOptionsMenu() называется.

Вместо обновления Intent как типов пользователей:

/*** 
    Copyright (c) 2012 CommonsWare, LLC 
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License. You may obtain  a copy 
    of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS,  WITHOUT  WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. 

    From _The Busy Coder's Guide to Android Development_ 
    http://commonsware.com/Android 
*/ 

package com.commonsware.android.sap; 

import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.EditText; 
import android.widget.Toast; 
import com.actionbarsherlock.app.SherlockActivity; 
import com.actionbarsherlock.view.Menu; 
import com.actionbarsherlock.widget.ShareActionProvider; 

public class MainActivity extends SherlockActivity implements 
    ShareActionProvider.OnShareTargetSelectedListener, TextWatcher { 
    private ShareActionProvider share=null; 
    private Intent shareIntent=new Intent(Intent.ACTION_SEND); 
    private EditText editor=null; 

    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.activity_main); 

    shareIntent.setType("text/plain"); 
    editor=(EditText)findViewById(R.id.editor); 
    editor.addTextChangedListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    getSupportMenuInflater().inflate(R.menu.actions, menu); 

    share= 
     (ShareActionProvider)menu.findItem(R.id.share) 
           .getActionProvider(); 
    share.setOnShareTargetSelectedListener(this); 

    return(super.onCreateOptionsMenu(menu)); 
    } 

    @Override 
    public boolean onShareTargetSelected(ShareActionProvider source, 
             Intent intent) { 
    Toast.makeText(this, intent.getComponent().toString(), 
        Toast.LENGTH_LONG).show(); 

    return(false); 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
    shareIntent.putExtra(Intent.EXTRA_TEXT, s.toString()); 
    share.setShareIntent(shareIntent); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
           int after) { 
    // ignored 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
          int count) { 
    // ignored 
    } 
} 

(как показано в this sample project)

+0

FYI есть (причудливая) проблема, http://stackoverflow.com/questions/22665226 с тремя полными дублирующимися вопросами и не одним ответом! – Fattie

Смежные вопросы