【问题标题】:How to pass edit text data in form of string to next activity?如何以字符串形式将编辑文本数据传递给下一个活动?
【发布时间】:2011-05-31 04:06:12
【问题描述】:

我正在开发一个 android 应用程序,其中我有两个按钮和一个编辑文本框。 我想通过单击其中一个按钮将字符串中的编辑文本框的数据传递给下一个活动,我如何将文本传递给下一个活动并在新启动的活动中接收该文本,以便可以使用该文本在那里面。

我的第一个活动代码是

EditText Urlis=(EditText)findViewById(R.id.entry);
final Button button = (Button) findViewById(R.id.ok);
final Intent i=new Intent(this , RSSReder.class);
final String choice=Urlis.getText().toString();

i.putExtra("key", choice);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(i);
    }    
});  

对于被调用的活动是

public class RSSReder extends Activity implements OnItemClickListener {

    public String RSSFEEDOFCHOICE;
    public final String tag = "RSSReader";
    private RSSFed feed = null;

    /** Called when the activity is first created. */

    public void onCreate(Bundle abc) {
        super.onCreate(abc);
        setContentView(R.layout.next1);

        Intent i = getIntent();
        RSSFEEDOFCHOICE =i.getStringExtra("key");

        // go get our feed!
        feed = getFeed(RSSFEEDOFCHOICE);

        // display UI
        UpdateDisplay();

    }
}

有什么我需要更改或删除的。

【问题讨论】:

  • 尝试输入“String choice=Urlis.getText().toString();”在“onClick()”方法中。还要放“i.putExtra("key",choice);"在“onClick()”之后。目前在哪里获得文本,很可能您将“选择”设置为空白字符串。当用户按下 Button 启动新 Activity 时,您需要获取文本。
  • 谢谢先生,我的代码应用程序正在运行,真的很有帮助

标签: android string button android-activity


【解决方案1】:

在您使用setContentView(...) 后,您需要引用您的EditText 并获取文本如...

EditText et = (EditText) findViewById(R.id.my_edit_text);
String theText = et.getText().toString();

要将其传递给另一个 Activity,请使用 Intent。示例...

Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_label", theText);
startActivity(i);

在新的 Activity(onCreate())中,您获取 Intent 并检索字符串...

public class MyNewActivity extends Activity {

    String uriString;

    @Override
    protected void onCreate(...) {

        ...

        Intent i = getIntent();
        uriString = i.getStringExtra("text_label");

    }
}

【讨论】:

  • +1,很好。先生也提到了发送和接收数据。
  • String theText = et.getText(); 会抛出编译时错误,因为getText() 返回一个Editable 对象。您还必须调用toString() 方法。
  • @Octavian:是的,先生!现在是清晨,我还没有喝到第一杯咖啡。错误已更正,感谢指出。
  • Thanx Mister 我传递了字符串并且它工作但现在的问题是编辑框的文本是我传递的 url 我希望它是公开的有没有办法我可以在之前得到它oncreate 是因为在 on create 中它会从其中删除 public 修饰符并且无法访问该站点,这是因为该 url 是一个 rss 提要,将被进一步传递以进行解析。
  • @Avi:所以只需将字符串设置为实例变量,您的 Activity 中的所有方法都可以访问它。请参阅我的答案中的最后一个代码块。
【解决方案2】:

在您的 Button 的 onClick 侦听器中尝试以下操作,

String str=editText.getEditableText().toString();

现在使用你的意图,

Intent intent=new Intent(this,nextActivity.this);
intent.putExtra("editText_value",str);
startActivity(intent);

【讨论】:

    【解决方案3】:

    您可以为此目的使用意图。这是相同的tutorial

    同时检查How to pass the values from one activity to previous activity

    在单击按钮时读取字符串的内容。

     EditText mText = (EditText)findViewById(R.id.edittext1);
    
    Button mButton = (Button)findViewById(R.id.button1);
    
    mButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                              String text = mText.getText.toString();
                              Intent intent = new Intent(this, Activity2.class);
                              intent.putExtra("key", text);
                              startActivity(intent);
                        }
                    });
    

    【讨论】:

      【解决方案4】:

      您必须使用 Intent 将数据传递给下一个活动。

      Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
      intent.putExtra("sampleString", "youstringdata");
      

      在 NextActivity 中:

      String sampleData = getIntent().getExtras().getLong("sampleString");

      【讨论】:

        猜你喜欢
        • 2013-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多