尝试将editText输出到shareIntent

时间:2013-09-05 20:41:14

标签: java android android-fragments

所以我是Android编程的新手,我正在尝试创建一个应用输入数据并将其保存到手机然后通过电子邮件发送出去的应用。我被卡住了。我的应用程序当前具有不保存的输入和可以获取文本字符串并将其作为文本消息中的文本字符串的共享按钮。这就是我到目前为止所知道的一切。

我现在的问题是:如何从输入(editText1)中获取文本并将其设置为纯文本到共享功能?

我尝试了很多我在网上看过的不同的东西,虽然我可以让它们变得没有错误,当我实际打开应用程序时,它会在我打开列表项时崩溃/停止。

这是我的片段代码:

package com.example.nextslidemenuapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


public class ItemDetailActivity extends FragmentActivity {

protected String mString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_detail);

    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // savedInstanceState is non-null when there is fragment state
    // saved from previous configurations of this activity
    // (e.g. when rotating the screen from portrait to landscape).
    // In this case, the fragment will automatically be re-added
    // to its container so we don't need to manually add it.
    // For more information, see the Fragments API guide at:
    //
    // http://developer.android.com/guide/components/fragments.html
    //
    if (savedInstanceState == null) {
        // Create the detail fragment and add it to the activity
        // using a fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, getIntent()
                .getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.item_detail_container, fragment).commit();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();

    if (itemId == R.id.action_share) {
        sharePost();
    }

    if (itemId == android.R.id.home) {
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpTo(this,
                new Intent(this, ItemListActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
final EditText ed1 = (EditText) findViewById(R.id.editText1);

String myString = "text";

private void sharePost() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, myString);
    startActivity(Intent.createChooser(shareIntent,    
getString(R.string.share_chooser_title)));

}
}

以下是我专注于编辑的具体代码:

final EditText ed1 = (EditText) findViewById(R.id.editText1);

String myString = "text";

private void sharePost() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, myString);
    startActivity(Intent.createChooser(shareIntent,
getString(R.string.share_chooser_title)));

}

我正在尝试用提取editText1的代码替换“text”。最终我想连接一些输入框和一些分号。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,试着在这里发布解决方案:

EditText ed1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);

ed1 = (EditText) findViewById(R.id.editText1);
}

private void sharePost() {
   Intent shareIntent = new Intent(Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(Intent.EXTRA_TEXT, ed1.getText().toString());
   startActivity(Intent.createChooser(shareIntent,
   getString(R.string.share_chooser_title)));
}

这不是最终解决方案,因为您的问题仍然非常模糊。但这就是它应该是这样的样子。底线:实例化editText,从editText中提取数据并通过shareIntent共享该文本。