麻烦从一个EditText送东西给使用意图另一个活动 [英] Trouble sending stuff from an EditText to another activity using intents

查看:75
本文介绍了麻烦从一个EditText送东西给使用意图另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦从我的编辑文本的数据转换成份额的意图extra.For某些原因,它返回即使在外地的EditText已经填满一个空字符串。         我知道有大约有麻烦了数据的编辑文本字段的人,但我似乎无法适应这些答案给我的code几个类似的线程。

I am having some trouble getting the data from my edit text into an share intent extra.For some reason , it is returning an empty string even when the EditText field has been filled. I Know there are several similar threads about people having trouble getting data out of an edit text field but I cant seem to adapt those answers to my code.

我附上了code中的相关位。做让我知道如果你需要看到更多的code(的code我无法与位在私人意图createShareIntent(),它是在所附$ C $下面c中的第三种方法)

I have attached the relevant bits of code .Do let me know if you need to see any more of the code.(the bits of code I am having trouble with is in the private Intent createShareIntent().It is the third method in the attached code below)

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.note_edit);//Set the layout file 
    setTitle(R.string.edit_note);//Set the title to be displayed in the action bar


    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.body);

   //get the row id of the note in the database form the intent .
    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }

    populateFields();

}
//THis method is used to create the menu in the Action bar
@Override//This works perfectly fine
public boolean onCreateOptionsMenu(Menu menu) {

    getSupportMenuInflater().inflate(R.menu.main, menu);//inflating the menu layout
    MenuItem actionItem = menu.findItem(R.id.menu_share);//The share button in handled here
    //The share action provider  
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    actionProvider.setShareHistoryFileName(null);
    actionProvider.setShareIntent(createShareIntent());//creating a share intent
    return true;//return true if the menu creation has been successful 
}

//Handles the share intent
private Intent createShareIntent() {//this is where the trouble is
    saveState();
    String body = mBodyText.getText().toString();//this returnes an empty string 
    String title = mTitleText.getText().toString();// this returnes an empty string
    Intent shareIntent = new Intent(Intent.ACTION_SEND);//Setting the action to ACTION_SEND
    shareIntent.setType("text/plain");//setting the text type to plain text
    shareIntent.putExtra(Intent.EXTRA_TEXT,body);//this sends an empty string
    shareIntent.putExtra(Intent.EXTRA_SUBJECT,title);//this sends an empty string
    return shareIntent;
}

我的问题是: 1.为什么我会收到来自EditText上,即使它填补了一个空字符串? 2.How解决呢?

My questions are: 1. Why am I getting an empty string from the edittext even when it is filled? 2.How do I fix it ?

感谢您抽出宝贵时间来阅读这一点,任何帮助,你可以给。

Thanks for taking the time to read this and for any help that you can give.

推荐答案

它返回空的文本,因为 putExtra 被称为内 onCreateOptionsMenu ,并在开始时(在创建选项菜单时),你的的EditText 是空的。您可以添加 TextChangedListener 您编辑文本和更新份额的意图,只要的EditText 的变化。

It is returning empty text because putExtra is called inside onCreateOptionsMenu and at the begining (when the option menu is being created) your EditText is empty. You can add TextChangedListener to your edit text and update your share intent whenever EditText changes.

et.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Update your intent here.
    }
});

这篇关于麻烦从一个EditText送东西给使用意图另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆