简体   繁体   English

单击android中的保存动作按钮将edittext值保存到另一个活动

[英]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 我的第一个活动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 它应该保存到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");
     }
}

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

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