我很难将编辑文本中的数据添加到共享意图中。由于某种原因,即使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.如何解决?
感谢您抽出宝贵的时间阅读本文档以及您可以提供的任何帮助。
它返回空文本,因为putExtra
它在内部被调用,onCreateOptionsMenu
并且在开始时(创建选项菜单时)您EditText
是空的。您可以TextChangedListener
在EditText
更改时添加到编辑文本并更新共享意向。
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Update your intent here.
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句