【问题标题】:Set sharing intent using icon in action bar使用操作栏中的图标设置共享意图
【发布时间】:2013-10-02 19:26:29
【问题描述】:

我想分享用户在我的应用程序的 web 视图中查看的网站,但是我遵循的任何教程都无法正常工作。我可以让图标加载到操作栏中,但它没有点击。我已经实现了 onclick 侦听器,但到目前为止还没有。该应用程序仅适用于 ICS 或更高版本,因此不需要向后兼容较旧的 android。我提交的代码是在实现任何共享图标之前,因为我想从头开始,或者看看是否有人对我能做什么有任何想法

   public class WebActivity extends Activity {

    private WebView mWebView = null;
    private EditText mInputUrl = null;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_web_view);

Intent intent = getIntent();
String thesite = intent.getStringExtra(MainPage.EXTRA_MESSAGE);

mInputUrl = (EditText)findViewById(R.id.input_url);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          String baseurl = "http://";
          String url = baseurl + mInputUrl.getText().toString();
          mWebView.loadUrl(url);
        }
    });

mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl(thesite);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
    public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
     }
 public void onProgressChanged(WebView view, int progress)   
 {
  //Make the bar disappear after URL is loaded, and changes string to Loading...
  MyActivity.setTitle("Loading...");
  MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

  // Return the app name after finish loading
     if(progress == 100)
        MyActivity.setTitle(R.string.app_name);
 }
 });
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
 // Check if the key event was the Back button and if there's history
WebView myWebView = (WebView) findViewById(R.id.webview);
 if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
 myWebView.goBack();
 return true;
 }
 // If it wasn't the Back key or there's no web page history, bubble up to the default
 // system behavior (probably exit the activity)
 return super.onKeyDown(keyCode, event);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
    // This ID represents the Home or Up button. In the case of this
    // activity, the Up button is shown. Use NavUtils to allow users
    // to navigate up one level in the application structure. For
    // more details, see the Navigation pattern on Android Design:
    //
    // http://developer.android.com/design/patterns/navigation.html#up-vs-back
    //
    NavUtils.navigateUpFromSameTask(this);
    return true;
}
return super.onOptionsItemSelected(item);
}
}

编辑

我已使用此代码尝试使其正常工作,但是当我单击该图标时,它根本没有做任何事情。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
return true;
}

@Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.menu_item_share:
        shareURL();
}
return super.onOptionsItemSelected(item);
}

private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This!"));
}

【问题讨论】:

    标签: android share


    【解决方案1】:

    我找到了答案。最后还是比较简单的。

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.share_menu, menu);
    return true;
    }
    
    @Override
    public final boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_item_share:
            shareURL();
    }
    if(item.getItemId() == R.id.menu_item_refresh){
        mWebView.reload();
        return true;
    }
    return super.onOptionsItemSelected(item);
    }
    
    private void shareURL() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
    startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
    }
    
    
    }
    

    这也让我也有一个刷新按钮。

    【讨论】:

      【解决方案2】:

      我没有看到任何对 onCreateOptionsMenu(..) 的覆盖,您确定您已添加 ActionItems 吗?看看http://developer.android.com/guide/topics/ui/actionbar.html

      阿里。

      【讨论】:

      • 我试过了。这是以前的阶段,我有点希望有人能给我一个例子
      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多