Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13.  
  14. public class MainActivity extends ActionBarActivity {
  15. public static SharedPreferences mySharedPreferences = null;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20.  
  21. // REFERENCE TO THE SAVE BUTTON IN THE XML-FILE
  22. Button btSave = (Button)findViewById(R.id.btSave);
  23. // ACTION LISTENER FOR THE BUTTON
  24. btSave.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. // READ TEXT FROM EDITTEXT-FIELD NUMBER 2 - FIRST GET A REFERENCE
  28. EditText etEditText2 = (EditText)findViewById(R.id.editText2);
  29. String insertedText = etEditText2.getText().toString();
  30. showToast(insertedText);
  31.  
  32. // SAVE THE DATA TO SHARED PREFERENCES
  33. mySharedPreferences = getPreferences(Context.MODE_PRIVATE);
  34. SharedPreferences.Editor editor = mySharedPreferences.edit();
  35. editor.putString("values", insertedText);
  36. editor.commit();
  37.  
  38. }
  39. });
  40. final Context thisContext = this;
  41. // GET REFERENCE TO THE SHOW DATA BUTTON
  42. Button btShowData = (Button)findViewById(R.id.btReadValues);
  43. // ADD ACTION HANDLER IN CASE BUTTON IS CLICKED
  44. btShowData.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. Intent intent = new Intent(thisContext, MainActivity2Activity.class);
  48. startActivity(intent);
  49. }
  50. });
  51.  
  52. }
  53.  
  54. // METHOD TO SHOW A TOAST
  55. public void showToast(String message) {
  56. Toast msg = Toast.makeText(this, message, Toast.LENGTH_LONG);
  57. msg.show();
  58. }
  59.  
  60. @Override
  61. public boolean onCreateOptionsMenu(Menu menu) {
  62. // Inflate the menu; this adds items to the action bar if it is present.
  63. getMenuInflater().inflate(R.menu.menu_main, menu);
  64. return true;
  65. }
  66.  
  67. @Override
  68. public boolean onOptionsItemSelected(MenuItem item) {
  69. // Handle action bar item clicks here. The action bar will
  70. // automatically handle clicks on the Home/Up button, so long
  71. // as you specify a parent activity in AndroidManifest.xml.
  72. int id = item.getItemId();
  73.  
  74. //noinspection SimplifiableIfStatement
  75. if (id == R.id.action_settings) {
  76. return true;
  77. }
  78.  
  79. return super.onOptionsItemSelected(item);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement