1

所以我是 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的代码替换“文本”。最终我想将一些输入框与一些分号连接在一起。

谢谢你的帮助!

4

1 回答 1

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共享该文本。

于 2013-09-05T22:13:50.467 回答