简体   繁体   中英

saving the edittext value to another activity on click of save actionbutton in android

how to save the text given to edit text, after clicking the save action button in action bar that should save to another activity. my first activity Add.java

public class Add extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        getActionBar().setDisplayShowHomeEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflate=getMenuInflater();
        getMenuInflater().inflate(R.menu.activity_add_action, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save:save();
                return true;
            case R.id.cancel:cancelnavi();
                return true;    
        }
        return super.onOptionsItemSelected(item);
    }

    private void save() {
        EditText titlename;
        titlename=(EditText)findViewById(R.id.edittitle);
        String title=titlename.getText().toString();
        if (title.equalsIgnoreCase("")){
            Toast.makeText(Add.this, "Script title should not be empty", Toast.LENGTH_LONG).show();
        } else {
            Intent i;
            i=new Intent(getApplicationContext(), Scripts.class);
            i.putExtra("n", titlename.getText().toString());
            startActivityForResult(i, 0);
            titlename.setText("");
        }
    }
}

It should be saved to scripts.java

public class Scripts extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scripts); 
        getActionBar().setDisplayShowHomeEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflate=getMenuInflater();
        getMenuInflater().inflate(R.menu.activity_main_actions, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_edit:editscript();
            break;  
        }
        return true;
    }

    private void editscript() {
        Intent editinIntent;
        editinIntent=new Intent(Scripts.this, Edit.class);
        startActivity(editinIntent);
    }
}

Maybe this can help you.

public class Scripts extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_scripts); 
         getActionBar().setDisplayShowHomeEnabled(false);



        //Extract the data

         String yourTitle = getIntent().getStringExtra("n");
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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