gpt4 book ai didi

java - 尝试将 editText 输出到 shareIntent

转载 作者:行者123 更新时间:2023-12-01 04:24:03 24 4
gpt4 key购买 nike

所以我是 Android 编程新手,我正在尝试制作一个应用程序来获取输入数据并将它们保存到手机上,然后通过电子邮件将它们发送出去。我目前陷入困境。我的应用程序当前具有不保存的输入和一个共享按钮,该按钮可以获取文本字符串并将其作为文本消息中的文本。这就是我到目前为止所了解到的全部内容。

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

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

这是我的有效 fragment 代码:

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”。最终我想将多个输入框与一些分号连接在一起。

感谢您的帮助!

最佳答案

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

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

关于java - 尝试将 editText 输出到 shareIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18645566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com