Android 分享按钮
Android share button
在我根据您的回答更新了我的问题后,这是我的。Class:
public class ShareScreen extends Activity{
private ShareScreenAdapter adapter;
private ViewPager viewPager;
private String[] mThumbIds = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_screen);
viewPager = (ViewPager) findViewById(R.id.pager);
ImageButton sharingButton = new ImageButton(this);
sharingButton.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sharingButton.setImageResource(R.drawable.share);
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new ShareScreenAdapter(ShareScreen.this, mThumbIds);
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareIt();
break;
default:
break;
}
return true;
}
private void shareIt()
{
//sharing implementation here
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
在 menu/main_share.xml 我有分享按钮 :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_share"
android:title="Share"
android:showAsAction="always"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
还是不行!问题是我的操作栏中的 shareButton 不工作。我点击它但没有任何反应!
没有堆栈跟踪很难调试,但从目前的代码来看,您实际上并没有将 sharingButton 添加到视图中。
[view you want to add the button to].addView(sharingButton)
我猜你想做的是下面的事情
首先,创建 onCreateOptionsMenu
和 onOptionsItemSelected
,它们适用于在 ActionBar
中单击的按钮。然后,您填写代码以匹配您的目标,如下所示:
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_id, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
shareit();
break;
default:
break;
}
return true;
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/shareButton"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
在我根据您的回答更新了我的问题后,这是我的。Class:
public class ShareScreen extends Activity{
private ShareScreenAdapter adapter;
private ViewPager viewPager;
private String[] mThumbIds = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_screen);
viewPager = (ViewPager) findViewById(R.id.pager);
ImageButton sharingButton = new ImageButton(this);
sharingButton.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sharingButton.setImageResource(R.drawable.share);
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new ShareScreenAdapter(ShareScreen.this, mThumbIds);
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareIt();
break;
default:
break;
}
return true;
}
private void shareIt()
{
//sharing implementation here
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
在 menu/main_share.xml 我有分享按钮 :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_share"
android:title="Share"
android:showAsAction="always"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
还是不行!问题是我的操作栏中的 shareButton 不工作。我点击它但没有任何反应!
没有堆栈跟踪很难调试,但从目前的代码来看,您实际上并没有将 sharingButton 添加到视图中。
[view you want to add the button to].addView(sharingButton)
我猜你想做的是下面的事情
首先,创建 onCreateOptionsMenu
和 onOptionsItemSelected
,它们适用于在 ActionBar
中单击的按钮。然后,您填写代码以匹配您的目标,如下所示:
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_id, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
shareit();
break;
default:
break;
}
return true;
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/shareButton"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>