您好我正在尝试开发和应用程序,需要从一个编辑文本框发送文本到另一个应用程序,如Kik,我已经做了共享菜单,我只需要让它从我的编辑文本框发送文本
继承我的共享菜单代码(XML):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_share"
android:title="Share"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:actionProviderClass= "android.widget.ShareActionProvider" />
</menu>
继承我的java:
package com.example.Encryptor_Kik;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
/**
* Created with IntelliJ IDEA.
* Date: 12/12/13
* Time: 3:15 PM
* To change this template use File | Settings | File Templates.
*/
public class Decryptor extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.decryption);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu2, menu);
MenuItem shareItem = menu.findItem(R.id.menu_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, "Hi");
mShare.setShareIntent(shareIntent);
return super.onCreateOptionsMenu(menu);
}
}
答案 0 :(得分:0)
问题是您的代码位于onCreateOptionsMenu
,在创建代码时,EditText
为空,因此text
为空。您需要添加一个用于更改文本的侦听器。这是完整的代码 -
public class Decryptor extends Activity {
EditText e;
private ShareActionProvider mShare;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.decryption);
e=(EditText)findViewById(R.id.YourID);
e.addTextChangedListener(commonTextWatcher);
TextWatcher commonTextWatcher
= new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
String text=e.getText.toString();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
if (mShare != null) {
mShare.setShareIntent(shareIntent);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu2, menu);
MenuItem shareItem = menu.findItem(R.id.menu_share);
String text=e.getText.toString();
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);
mShare.setShareIntent(shareIntent);
return super.onCreateOptionsMenu(menu);
}
}