15 ActionProvider代码例子

  • Menu文件夹下代码:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <!-- 
        系统提供的分享: android:actionProviderClass="包名.类名"
        android:actionProviderClass="android.widget.ShareActionProvider"
        -->
        <item
            android:id="@+id/action_shared"
            android:orderInCategory="100"
            android:showAsAction="always"
            android:actionProviderClass="android.widget.ShareActionProvider"
            />
        <item
            android:id="@+id/action_custom"
            android:orderInCategory="100"
            android:showAsAction="always"
            android:actionProviderClass="com.qf.day15_actionbar_demo3.MyProvider"
            />
    
    </menu>
    
    • 界面xml代码:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内涵段子" />
    
    </RelativeLayout>
    
  • MainActivity界面逻辑代码:

    package com.qf.day15_actionbar_demo3;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ShareActionProvider;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.tv);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
    
            MenuItem item = menu.findItem(R.id.action_shared);
            ShareActionProvider provider = (ShareActionProvider) item.getActionProvider();
    
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");//分享的数据类型   文本
            //分享的数据
            shareIntent.putExtra(Intent.EXTRA_TEXT, tv.getText().toString().trim());
    
            provider.setShareIntent(shareIntent);
    
            return true;
        }
    
    }
    
  • 一个自定义ActionProvider代码:

    package com.qf.day15_actionbar_demo3;
    
    import android.content.Context;
    import android.view.ActionProvider;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MyProvider extends ActionProvider {
    
        private Context context;
    
    
        public MyProvider(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            this.context = context;
        }
    
        @Override
        @Deprecated
        public View onCreateActionView() {
            // TODO Auto-generated method stub
            TextView tv = new TextView(context);
    
            tv.setText("分享");
    
            tv.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "自定义的分享", 0).show();
                }
            });
    
            return tv;
        }
    
    }
    

posted on 2016-09-09 19:04  木鱼哥  阅读(264)  评论(0编辑  收藏  举报

导航