从编辑文本中获取文本并在文本视图中写入

发布于 2024-11-06 06:33:01 字数 596 浏览 0 评论 0 原文

我有以下代码:

public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accounted);

        TextView tv1 = (TextView) findViewById(R.id.textView1);
        EditText et1 = (EditText) findViewById(R.id.editText1);
        String text1 =  et1.getText().toString();

当我注释掉“string text1...”行时,程序可以正常工作。但如果我取消注释,应用程序就会关闭。

TextView(tv1) 位于当前页面,但 EditText(et1) 位于另一页面。这可能是我的问题的原因吗?我该如何解决这个问题?

I have the following code:

public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accounted);

        TextView tv1 = (TextView) findViewById(R.id.textView1);
        EditText et1 = (EditText) findViewById(R.id.editText1);
        String text1 =  et1.getText().toString();

When I comment out the "string text1..." line the program works. But if I uncomment it, the application force closes.

TextView(tv1) is on the current page but EditText(et1) is on another page. Can that be the cause of my problem? How do I work around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿嘿嘿 2024-11-13 06:33:02

意图示例将文本视图的值传递给第二个活动

您无法从另一个活动访问文本视图,您应该使用如何在 Android 应用程序中的 Activity 之间传递数据?

You can't access the textview from another activty you should pass the value of the textview to the second activity using intents

example available at How do I pass data between Activities in Android application?

ぇ气 2024-11-13 06:33:02

如果 EditText 位于不同的 xml 中,则不要使用它。
在 xml 中添加 EditText 或从源代码中删除它。

If EditText is in different xml then dont use it.
Add EditText either in your xml or remove it from source code.

血之狂魔 2024-11-13 06:33:02

这是你的代码......................

public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accounted);

    TextView tv1 = (TextView) findViewById(R.id.textView1);
    EditText et1 = (EditText) findViewById(R.id.editText1);
    String text1 =  et1.getText().toString();

到目前为止,我让你在另一个活动中拥有编辑文本。
因此,为了在文本视图中获取文本,您必须进行一些修改
我正在展示它,希望这会起作用,

首先在当前活动中添加一个编辑文本,其中文本视图位于

editText t1;

此分配此编辑文本之后,另一个活动的编辑文本

t1=secondactivity.edittext id;

现在只需获取该文本并在您的文本视图上设置

 String text1 =  et1.getText().toString();
 text1.setText(et1.getText().toString());

this is your code...................

public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accounted);

    TextView tv1 = (TextView) findViewById(R.id.textView1);
    EditText et1 = (EditText) findViewById(R.id.editText1);
    String text1 =  et1.getText().toString();

as far i m getting you have the edittext in another activity.
so for getting text in your textview you have to do some modifications
i am showing it hope this will work

first add a edittext in your current activity in which the textview is

editText t1;

after this assign this editText the edittext of another activity

t1=secondactivity.edittext id;

now simply get that text and set on your textview

 String text1 =  et1.getText().toString();
 text1.setText(et1.getText().toString());
向日葵 2024-11-13 06:33:02
 public class Activity2 extends Activity {
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.accounted);

            TextView tv1 = (TextView) findViewById(R.id.textView1);
            EditText et1 = (EditText) findViewById(R.id.editText1);
            String text1 =  et1.getText().toString();

这是您的代码。您在另一个活动中有 Edittext,这就是应用程序崩溃的原因。如果您想从上一个活动获取 edittext 的文本以在文本视图上设置,您必须将以下行添加到使用 Intent 进行更改的代码中活动。

 Intent.putextra("string1",et1.getText().toString());
    StartActivity(Intent);

这肯定对你有用。

 public class Activity2 extends Activity {
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.accounted);

            TextView tv1 = (TextView) findViewById(R.id.textView1);
            EditText et1 = (EditText) findViewById(R.id.editText1);
            String text1 =  et1.getText().toString();

This is your code .You have Edittext in another activity thats why the app crashes.If you want to get the text of edittext from last activity to set on textview you will have to add the following line to the code where Intent is used to change the activity.

 Intent.putextra("string1",et1.getText().toString());
    StartActivity(Intent);

This will definately work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文