1

我正在尝试将输入的值保存到表单中,以便当用户返回活动时输入的内容保持填充状态。因为它现在起作用了。用户可以输入详细信息并单击“发送按钮”。下一个活动将显示用户输入内容的详细信息。如果用户选择“后退按钮”,用户会返回上一个活动,但会从表单中的字段中擦除内容。任何建议,将不胜感激。

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
     public final static String EXTRA_FROM = "com.example.assignment1.FROM";
     public final static String EXTRA_TO = "com.example.assignment1.TO";
     public final static String EXTRA_CC = "com.example.assignment1.CC";
     public final static String EXTRA_SUBJECT = "com.example.assignment1.SUBJECT";
     public final static String EXTRA_COMPOSE = "com.example.assignment1.COMPOSE";



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        String from = emailFrom.getText().toString();
        outState.putString(EXTRA_FROM, from);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedState)
    {
        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        String from = savedState.getString(EXTRA_FROM);
        emailFrom.setText(from);
    }
    public void emailSend (View sendButton) 
    {
        Intent intent = new Intent(this,DisplayEmailActivity.class);
        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        EditText emailTo =(EditText) findViewById(R.id.editEmailTo);
        EditText emailCc =(EditText) findViewById(R.id.editEmailCc);
        EditText emailSubject =(EditText) findViewById(R.id.editEmailSubject);
        EditText emailCompose =(EditText) findViewById(R.id.editEmailCompose);

        String from = emailFrom.getText().toString();
        String to = emailTo.getText().toString();
        String cc = emailCc.getText().toString();
        String subject = emailSubject.getText().toString();
        String compose = emailCompose.getText().toString();

        intent.putExtra(EXTRA_FROM,from);
        intent.putExtra(EXTRA_TO,to);
        intent.putExtra(EXTRA_CC,cc);
        intent.putExtra(EXTRA_SUBJECT,subject);
        intent.putExtra(EXTRA_COMPOSE,compose);

        startActivity(intent);
    }



    public void emailClear (View clearButton) {
         ((EditText) findViewById(R.id.editEmailFrom)).setText("");
         ((EditText) findViewById(R.id.editEmailTo)).setText("");
         ((EditText) findViewById(R.id.editEmailCc)).setText("");
         ((EditText) findViewById(R.id.editEmailBcc)).setText("");
         ((EditText) findViewById(R.id.editEmailSubject)).setText("");
         ((EditText) findViewById(R.id.editEmailCompose)).setText("");  
    }

}

第二次活动……

public class DisplayEmailActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_email);

        Intent intent = getIntent();

        String from = intent.getStringExtra(MainActivity.EXTRA_FROM);
        String to = intent.getStringExtra(MainActivity.EXTRA_TO);
        String cc = intent.getStringExtra(MainActivity.EXTRA_CC);
        String subject = intent.getStringExtra(MainActivity.EXTRA_SUBJECT);
        String compose = intent.getStringExtra(MainActivity.EXTRA_COMPOSE);


        TextView textFrom =(TextView)findViewById(R.id.displayEmailFrom);
        TextView textTo =(TextView)findViewById(R.id.displayEmailTo);
        TextView textCc =(TextView)findViewById(R.id.displayEmailCc);
        TextView textSubject =(TextView)findViewById(R.id.displayEmailSubject);
        TextView textCompose =(TextView)findViewById(R.id.displayEmailCompose);

        textFrom.setText(from);
        textTo.setText(to);
        textCc.setText(cc);
        textSubject.setText(subject);
        textCompose.setText(compose);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_display_email, menu);
        return true;
    }

    public void emailBack (View backButton){
        Intent intent = new Intent(this,MainActivity.class);

        startActivity (intent);
    }


}
4

3 回答 3

3

emailBack开始一个全新的活动。只是finish打电话

于 2012-10-10T12:07:39.303 回答
0

使用共享首选项...要创建字符串值,请使用 -

String string12 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("butname12", " "); 

如果你想改变它或重写 -

getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                    .edit()
                    .putString("butname12", "0" )
                    .commit(); 

调用保存值的​​含义-

String ss = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("butname12", " ");
于 2012-10-10T12:11:36.650 回答
0

换成这样的方法,

public void emailBack (View backButton){

    onBackPressed();
}
于 2012-10-10T12:11:48.087 回答