简体   繁体   English

GetExtra PutExtra

[英]GetExtra PutExtra

Ok So i want to pass values from One activity to another using getextrand putextra method. 好的,所以我想使用getextrand putextra方法将值从一个活动传递到另一个活动。

In Second Activity in which I want to receive data is full of contents like Buttons and Text view. 在我想接收数据的第二活动中,充满了诸如按钮和文本视图之类的内容。 and i want to set that certain value which i have received from MainActivity to a particular text box. 我想将我从MainActivity收到的某个值设置为特定的文本框。

setContenView(R.id.intent)

is the easiest one method to show a string but what if I want to Set this value to one or more textview. 是显示字符串的最简单的一种方法,但是如果我想将此值设置为一个或多个textview,该怎么办。 My code is here 我的代码在这里

MainActivity 主要活动

package com.prashant.cookbook;

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


public class MainActivity extends Activity {


static String Message_send="Prashant";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText et=(EditText)findViewById(R.id.editText1);
    Button send=(Button)findViewById(R.id.send);

    final Intent msg_send= new Intent(this,Second.class);
    String MSG= et.getText().toString();
    msg_send.putExtra(Message_send, MSG);
    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(msg_send);


        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} }

SecondAvtivity SecondAvtivity

package com.prashant.cookbook;

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

public class Second extends Activity {

private TextView tv;
private Intent rcv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    tv = (TextView) findViewById(R.id.msg_show);
    rcv = getIntent();
    String Show_msg;
    Show_msg=rcv.getStringExtra(MainActivity.Message_send);
    tv.setText(Show_msg);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

} }

but when I run this code I got nothing but a blank second Activity Not even a default text 但是当我运行这段代码时,我什么也没有,只有空白的第二个活动,甚至没有默认文本

get Value entered by user in EditText on Button click and then use msg_send.putExtra for placing value in Intent as: 获取用户在单击按钮时在EditText中输入的值,然后使用msg_send.putExtra将值放置在Intent中,如下所示:

send.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

       String MSG= et.getText().toString();  //<< get value from EditText here
       msg_send.putExtra(Message_send, MSG);            
       startActivity(msg_send);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM