首页 文章

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

提问于
浏览
272

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

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );

然后在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 
        }

    }

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

谢谢,

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

14 回答

  • 43

    把功能

    etname=(EditText)findViewById(R.id.Name);
            tvname=(TextView)findViewById(R.id.tvName);
    
            b1= (ImageButton) findViewById(R.id.Submit);
    
            b1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    String s=etname.getText().toString();
    
                    Intent ii=new Intent(getApplicationContext(), MainActivity2.class);
                    ii.putExtra("name", s);
                    Toast.makeText(getApplicationContext(),"Page 222", Toast.LENGTH_LONG).show();
                    startActivity(ii);
                }
            });
    
    
    
    getfunction 
    
    public class MainActivity2 extends Activity {
        TextView tvname;
        EditText etname;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_activity2);
            tvname = (TextView)findViewById(R.id.tvName);
            etname=(EditText)findViewById(R.id.Name);
            Intent iin= getIntent();
            Bundle b = iin.getExtras();
    
            if(b!=null)
            {
    
              String j2 =(String) b.get("name");
    
    etname.setText(j2);
                Toast.makeText(getApplicationContext(),"ok",Toast.LENGTH_LONG).show();
            }
        }
    
  • 0

    将字符串放入Intent对象中

    Intent intent = new Intent(FirstActivity.this,NextAcitivity.class);
      intent.putExtra("key",your_String);
      StartActivity(intent);
    

    onCreate方法中的NextAcitvity获取String

    String my_string=getIntent().getStringExtra("key");
    

    这是简单而简短的方法

  • 5

    Android在Intent类中引入了新方法 .

    • 使用 hasExtra() 检查intent是否包含密钥数据 .

    • 您可以直接使用 getStringExtra() .

    Pass Data

    intent.putExtra(USER_NAME, "user");
    

    Get Data

    String userName;
    if (getIntent().hasExtra(USER_NAME)) {
        userName = getIntent().getStringExtra(USER_NAME);
    }
    

    I always put keys in constants for best practice (to avoid manual errors)

    public interface PutExtraConstants {
        String USER_NAME = "USER_NAME";
    }
    
  • 14

    Push Data

    import android.content.Intent;
    
        ...
    
        Intent intent = 
            new Intent( 
                this, 
                MyActivity.class );
        intent.putExtra( "paramName", "paramValue" );
        startActivity( intent );
    

    上面的代码可能在主 activity 内 . “ MyActivity.class ”是我们要发布的第二个 Activity ;它必须明确包含在 AndroidManifest.xml 文件中 .

    <activity android:name=".MyActivity" />
    

    Pull Data

    import android.os.Bundle;
    
        ...
    
        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            String myParam = extras.getString("paramName");
        }
        else
        {
            //..oops!
        }
    

    在此示例中,上面的代码将位于 MyActivity.java 文件中 .

    陷阱

    此方法只能传递 strings . 所以,假设您需要将 ArrayList 传递给 ListActivity ;一种可能的解决方法是传递逗号分隔字符串,然后在另一端分割它 .

    替代方案

    使用 SharedPreferences

  • 6

    最佳方法......

    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);
    

    RecievingActivity

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

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

    String data = getIntent().getExtras().getString("keyName","defaultKey");
    

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

  • 1

    更简单

    发件人方

    Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
    i.putExtra("id","string data");
    startActivity(i)
    

    接收方

    Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
    String strData = i.getStringExtra("id");
    
  • 364

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

    Intent i = new Intent(FirstScreen.this,SecondScreen.class);
    String strName = null;
    i.putExtra(Intent.EXTRA_TEXT,strName);
    然后,要检索值,请尝试类似:String newString;
    Bundle extras = getIntent() . getExtras();
    if(extras == null)
    else

  • 2

    简单,在第一个活动中 -

    EditText name= (EditText) findViewById(R.id.editTextName);
        Button button= (Button) findViewById(R.id.buttonGo);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Main2Activity.class);
                i.putExtra("name",name.getText().toString());
               startActivity(i);
              }
        });
    

    在第二个活动中 -

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        TextView t = (TextView) findViewById(R.id.textView);
        Bundle bundle=getIntent().getExtras();
        String s=bundle.getString("name");
        t.setText(s);
    }
    

    如果需要,您可以添加if / else条件 .

  • 1

    用它来“放”文件......

    Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
    String strName = null;
    i.putExtra("STRING_I_NEED", strName);
    

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

    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");
    }
    
  • 4

    把字符串放在首位

    Intent secondIntent = new Intent(this, typeof(SecondActivity));
                secondIntent.PutExtra("message", "Greetings from MainActivity");
    

    之后检索它

    var message = this.Intent.GetStringExtra("message");
    

    就这样 ;)

  • 0

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

    发送数据

    intent = new Intent(getActivity(), CheckinActivity.class);
        intent.putExtra("mealID", meal.Meald);
        startActivity(intent);
    

    获取数据

    int mealId;
    
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
    
        if(bundle != null){
            mealId = bundle.getInt("mealID");
        }
    

    干杯!

  • 1

    first 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);
        }
    });
    

    Second 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);
            }
        }
    }
    
  • 0

    在Android中实现 intent 非常容易 . 它会将您从一个活动转移到另一个活动,我们需要两个方法 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);
    

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

    String data = getIntent().getExtras().getString("AnyKeyName");
            textview.setText(data);
    

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

  • 61
    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);
    

    ApplicationActivity

    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");
    }
    

相关问题