活动的共享首选项?

时间:2014-05-13 14:23:50

标签: android eclipse

我有3项活动

  1. 设置
  2. 其他人的紧急情况
  3. 我已经在设置中保存了共享偏好设置。但问题是,当我点击保存按钮时,它会将其保存并返回主要活动。主要活动中有一个退出按钮。当我点击它时。返回设置活动而不是关闭应用程序。

    这是设置活动的代码:

    @SuppressLint("ShowToast")   
    public class Setting extends Activity {
    
    TextView name ;
    TextView mobile;
    
    public static final String Blood = "BloodDonor";
    public static final String Name = "namekey";
    public static final String Mobile="mobileKey";
    
    SharedPreferences sharedpreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    
         name = (TextView) findViewById(R.id.editText1);
         mobile = (TextView) findViewById(R.id.editText3);
    
         sharedpreferences = getSharedPreferences(Blood, MODE_PRIVATE);
             if (sharedpreferences.contains(Name))
             {
                 name.setText(sharedpreferences.getString(Name, null));
    
             }
             if (sharedpreferences.contains(Mobile))
             {
                mobile.setText(sharedpreferences.getString(Mobile, null));
             } 
    
    
    }
    
        public void run (View view)
        {
            String n = name.getText().toString();
            String m = mobile.getText().toString();
            Editor editor = sharedpreferences.edit();
            editor.putString(Name, n);
            editor.putString(Mobile, m);
    
            editor.commit();
        }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.setting, menu);
        return true;
    }
    
    
    }
    

    这是主要活动的代码:

    public class Main extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.btnSetting);
        button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(),Setting.class);
                startActivity(i);
            }
        });
    
        Button btnExit=(Button)findViewById(R.id.btnexit);
        btnExit.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                int pid = android.os.Process.myPid();
                android.os.Process.killProcess(pid);
                 System.exit(0);
            }
        });
    
        Button btnEmerForOthers=(Button)findViewById(R.id.btnEmerForOthers);
        btnEmerForOthers.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(),EmergencyForOthers.class);
                startActivity(i);
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    }
    

1 个答案:

答案 0 :(得分:0)

finish() Main中致电Activity

btnExit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        finish();
    }
});