How to pass edit text data in form of string to next activity?

前端 未结 4 2038
醉话见心
醉话见心 2020-12-09 05:39

I am developing an android application in which i have taken two buttons and one edit text box. i want to pass the data of edit text box in from of string to the next activi

相关标签:
4条回答
  • 2020-12-09 05:55

    You can use intents for the purpose. Here's a tutorial for the same.

    Also check How to pass the values from one activity to previous activity

    Reading the contents of a String on Button click.

     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);
                        }
                    });
    
    0 讨论(0)
  • 2020-12-09 05:57

    After you have used setContentView(...) you need to reference your EditText and get the text such as...

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

    To pass it to another Activity you use an Intent. Example...

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

    In the new Activity (in onCreate()), you get the Intent and retrieve the String...

    public class MyNewActivity extends Activity {
    
        String uriString;
    
        @Override
        protected void onCreate(...) {
    
            ...
    
            Intent i = getIntent();
            uriString = i.getStringExtra("text_label");
    
        }
    }
    
    0 讨论(0)
  • 2020-12-09 05:57

    Inside your Button's onClick Listener try the following,

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

    Now use your intent,

    Intent intent=new Intent(this,nextActivity.this);
    intent.putExtra("editText_value",str);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-09 05:58

    You have to use Intent to pass data to the next activity.

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

    In NextActivity:

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

    0 讨论(0)
提交回复
热议问题