如何使用putExtra()和getExtra()来表示字符串数据

Sha*_*aaz 298 android android-intent

有人可以告诉我如何使用getExtra()putExtra()意图吗?实际上我有一个字符串变量,比如str,它存储一些字符串数据.现在,我想将这些数据从一个活动发送到另一个活动.

  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );
Run Code Online (Sandbox Code Playgroud)

然后在SecondScreen.java中

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        TextView userName = (TextView)findViewById(R.id.userName);
        Bundle bundle = getIntent().getExtras();

        if(bundle.getString("strName")!= null)
        {
            //TODO here get the string stored in the string variable and do 
            // setText() on userName 
        }

    }
Run Code Online (Sandbox Code Playgroud)

我知道这是一个非常基本的问题但不幸的是我被困在这里.请帮忙.

谢谢,

编辑:这里我试图从一个屏幕传递到另一个屏幕的字符串是动态的.那就是我有一个editText,我得到的字符串无论用户类型如何.然后借助于myEditText.getText().toString().我将输入的值作为字符串,然后我必须传递此数据.

Wil*_*ate 392

用它来"放"文件......

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);
Run Code Online (Sandbox Code Playgroud)

然后,要检索值,请尝试以下方法:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
Run Code Online (Sandbox Code Playgroud)

  • 是用于处理方向变化的"savedInstanceState ..."和"... getSerialiable"代码?如果不是,该代码用于什么? (10认同)

小智 64

首先是Screen.java

text=(TextView)findViewById(R.id.tv1);
edit=(EditText)findViewById(R.id.edit);
button=(Button)findViewById(R.id.bt1);

button.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        String s=edit.getText().toString();

        Intent ii=new Intent(MainActivity.this, newclass.class);
        ii.putExtra("name", s);
        startActivity(ii);
    }
});
Run Code Online (Sandbox Code Playgroud)

第二个Screen.java

public class newclass extends Activity
{
    private TextView Textv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.intent);
        Textv = (TextView)findViewById(R.id.tv2);
        Intent iin= getIntent();
        Bundle b = iin.getExtras();

        if(b!=null)
        {
            String j =(String) b.get("name");
            Textv.setText(j);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Xar*_*mer 47

最佳方法......

SendingActivity

Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
intent.putExtra("keyName", value);  // pass your values and retrieve them in the other Activity using keyName
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

RecievingActivity

 Bundle extras = intent.getExtras();
    if(extras != null)
    String data = extras.getString("keyName"); // retrieve the data using keyName 
Run Code Online (Sandbox Code Playgroud)

///接收数据的最短路径..

String data = getIntent().getExtras().getString("keyName","defaultKey");
Run Code Online (Sandbox Code Playgroud)

//这需要api 12. //第二个参数是可选的.如果keyName为null,则使用defaultkeyas数据.


Sin*_*Þór 16

这就是我一直在使用的,它可以帮助某人......简单而有效.

发送数据

    intent = new Intent(getActivity(), CheckinActivity.class);
    intent.putExtra("mealID", meal.Meald);
    startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

获取数据

    int mealId;

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if(bundle != null){
        mealId = bundle.getInt("mealID");
    }
Run Code Online (Sandbox Code Playgroud)

干杯!


小智 9

它很容易intent在Android中实现..它需要你从一个活动转移到另一个活动,我们有两个方法putExtra();,getExtra();现在我给你看示例..

    Intent intent = new Intent(activity_registration.this, activity_Login.class);
                intent.putExtra("AnyKeyName", Email.getText().toString());  // pass your values and retrieve them in the other Activity using AnyKeyName
                        startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

现在我们必须从AnyKeyName参数中获取值,下面提到的代码将有助于这样做

       String data = getIntent().getExtras().getString("AnyKeyName");
        textview.setText(data);
Run Code Online (Sandbox Code Playgroud)

Intent无论我们需要什么,我们都可以轻松设置接收值.


ser*_*inc 6

一个小附录:你不必为密钥创建自己的名字,android提供这些,f.ex.Intent.EXTRA_TEXT.修改接受的答案:

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra(Intent.EXTRA_TEXT, strName);
Run Code Online (Sandbox Code Playgroud)

然后,要检索值,请尝试以下方法:

String newString;
Bundle extras = getIntent().getExtras();
if(extras == null) {
    newString= null;
} else {
    newString= extras.getString(Intent.EXTRA_TEXT);
}
Run Code Online (Sandbox Code Playgroud)


小智 6

发送

startActivity(new Intent(First.this, Secend.class).putExtra("key",edit.getText.tostring));
Run Code Online (Sandbox Code Playgroud)

得到

String myData = getIntent.getStringExtra("key");
Run Code Online (Sandbox Code Playgroud)


Sam*_*RAK 5

Intent intent = new Intent(view.getContext(), ApplicationActivity.class);
                        intent.putExtra("int", intValue);
                        intent.putExtra("Serializable", object);
                        intent.putExtra("String", stringValue);
                        intent.putExtra("parcelable", parObject);
                        startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

应用活动

Intent intent = getIntent();
Bundle bundle = intent.getExtras();

if(bundle != null){
   int mealId = bundle.getInt("int");
   Object object = bundle.getSerializable("Serializable");
   String string = bundle.getString("String");
   T string = <T>bundle.getString("parcelable");
}
Run Code Online (Sandbox Code Playgroud)


Khe*_*raj 5

Update in Intent class.

  • Use hasExtra() for checking if intent has data on key.
  • You can use now getStringExtra() directly.

Pass Data

intent.putExtra(PutExtraConstants.USER_NAME, "user");
Run Code Online (Sandbox Code Playgroud)

Get Data

String userName;
if (getIntent().hasExtra(PutExtraConstants.USER_NAME)) {
    userName = getIntent().getStringExtra(PutExtraConstants.USER_NAME);
}
Run Code Online (Sandbox Code Playgroud)

Always put keys in constants as best practice.

public interface PutExtraConstants {
    String USER_NAME = "USER_NAME";
}
Run Code Online (Sandbox Code Playgroud)