【问题标题】:Does anyone know how to get the value of edittext from one activity to a textvew in another using a button?有谁知道如何使用按钮将edittext的值从一个活动中获取到另一个活动中的textvew?
【发布时间】:2011-11-12 06:19:37
【问题描述】:

使用按钮将 EDITTEXT 输入到另一个类中的 TEXTVIEW

我对 android 相当陌生,我正在尝试使用 edittext 在一个屏幕(活动)上获取用户输入,实际上不仅仅是一个 edittext,一些像几个编辑文本,也许还有一个微调器,有点像创建一个新的用户屏幕。但是我知道如何使用按钮从edittext到textview的getText()和setText(),如果它们在同一个活动中但找不到任何地方如何完成这个。这就像头等舱的基本内容:

public class UserInput extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        EditText edit = (EditText) findViewById(R.id.editText1);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });
    }
}

现在我知道魔法将在 onClick(View v){} 方法中,但我究竟使用什么魔法来 1-打开一个包含 textview 的新 Activity 和 2-打开 Activity?
这是第二个 Activity 供视觉参考:

public class GetText extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        TextView view = (TextView) findViewById(R.id.textView1);
    }
}

如果有人甚至可以砍掉我将使用的代码,请再次请求它现在就可以工作。希望每个人都能团结起来并提供意见,以帮助其他可能陷入困境的人。提前谢谢。


这就是我所拥有的,如果力量对我关闭:

主要活动:

package com.mandam.ok;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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

            EditText edit = (EditText) findViewById(R.id.editText1);
            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText edit = (EditText)            
                            findViewById(R.id.editText1);
            Intent intent = new Intent(UserInput.this, GetText.class);
            intent.putExtra("com.mandam.ok.GETTEXT",                         
                            edit.getText().toString());
            startActivity(intent);
                }
            });
        }
    }

第二个活动:

package com.mandam.ok;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class GetText extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        TextView view = (TextView) findViewById(R.id.textView1);
        Intent intent = getIntent();
        String name = intent.getStringExtra("com.mandam.ok.MAIN");
        //edit.setText(view.getText());
    }
}

我什至会附上我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

第二个xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mandam.ok"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".OkActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".GetText" >
            <intent-filter >
                <action android:name="com.mandam.ok.GETTEXT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

现在我知道我错过了一些非常简单的东西,只是不知道是什么。我很感激到目前为止的帮助。 :)

【问题讨论】:

  • 我想我不明白。因此,您的主要活动有一个 EditText。是否要将这个 EditText 的文本放入一个新的 Activity 并加载到新的 Activity 的 EditText 中?
  • 不,如果您查看活动二,它是一个文本视图。我想从编辑文本中获取用户输入并将该输入放入单独活动的文本视图中,如果有人正在为雅虎邮件设置帐户,那么该人有填写姓名、地址、密码等的信息。然后那个人点击提交按钮,然后在下一页上显示所有信息,类似这样。

标签: android android-intent


【解决方案1】:

您的第二个活动有一个文本视图...说它是一个文本视图是错误的,因为活动不是视图。

以下是向新 Activity 传递参数的方法...此代码将在您的第一个 Activity 的 onClick 中:

EditText edit = (EditText) findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.package.name.NAME", edit.getText().toString());
startActivity(intent);

“com.package.name”是你的包名。意图是执行操作的抽象。在这种情况下,intent 告诉 android 创建一个新的 Activity。 putExtra 方法允许您在告诉 Android 创建新 Activity 之前将额外信息放入 Intent。当您将一个额外的变量放入一个意图时,您需要为其提供一个唯一的字符串标识符,该标识符最好以包名称开头。

创建另一个 Activity 后,您可以通过以下方式检索字符串:

Intent intent = getIntent();
String name = intent.getStringExtra("com.package.name.NAME");
// do whatever you want with name

【讨论】:

  • 嗨,好的,我很抱歉我是个菜鸟,但我试过了,我的应用程序将在我尝试打开它时强制关闭我将完全按照我必须显示的方式附加所有代码你我有什么:
  • aleph_null,好的,我已经提供了我的代码,同时将您的代码合并到其中,它会强制关闭,您能否对此有所了解。谢谢
  • @mandam:在您的第一个活动中,您将放置一个标识符为com.mandam.ok.GETTEXT 的字符串。但是,在您的第二个活动中,您正尝试检索标识符为 com.mandam.ok.MAIN 的字符串。这些标识符需要匹配,否则你会得到一个错误。一个不太重要的变化是您不必定义EditText edit 两次。只需在 onClick 中这样做就可以了。
  • aleph_null,好的,我知道为什么要强制关闭主要活动没有定义,但我修复了它并修复了我的字符串标识符,但它仍然没有显示输入到编辑文本中的文本下一个活动。
  • @mandam:你打电话给view.setText(name);吗?如果是这样,调用getStringExtra 后name 的值是多少?我建议阅读调试以及日志功能来帮助您解决这些类型的问题。
猜你喜欢
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 2017-09-30
  • 2013-09-24
相关资源
最近更新 更多