【问题标题】:Trouble sending stuff from an EditText to another activity using intents使用意图将内容从 EditText 发送到另一个活动时遇到问题
【发布时间】:2013-07-24 13:16:54
【问题描述】:

我在将数据从我的编辑文本中获取到额外的共享意图时遇到了一些问题。出于某种原因,即使 EditText 字段已被填充,它也会返回一个空字符串。 我知道有几个类似的线程关于人们无法从编辑文本字段中获取数据,但我似乎无法使这些答案适应我的代码。

我已附上相关的代码。如果您需要查看更多代码,请告诉我。(我遇到问题的代码在私有 Intent createShareIntent() 中。这是第三个下面附上代码中的方法)

   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.如何解决?

感谢您抽出宝贵时间阅读本文并提供任何帮助。

【问题讨论】:

  • 愚蠢的问题,你的 EditTexts 包含什么吗?
  • 当您说“返回一个空字符串”时,您的意思是您没有收到任何东西,还是您实际上尝试使用Toast 显示它?
  • 我尝试用吐司显示它并尝试在日志中打印它。我只是在日志中得到一个空白区域和一个空吐司。
  • @blackbelt 是的。

标签: android android-activity android-edittext


【解决方案1】:

它返回空文本,因为在 onCreateOptionsMenu 内部调用了 putExtra 并且在开始时(创建选项菜单时)您的 EditText 为空。您可以将TextChangedListener 添加到您的编辑文本中,并在EditText 更改时更新您的分享意图。

et.addTextChangedListener(new TextWatcher() {

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

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多