Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. import android.content.Intent;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Spinner;
  10. import android.widget.Toast;
  11.  
  12. public class SecondActivity extends AppCompatActivity implements
  13. AdapterView.OnItemSelectedListener {
  14. Button btn2;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_second);
  20. btn2 = (Button)findViewById(R.id.button1);
  21. btn2.setOnClickListener(new View.OnClickListener() {
  22. public void onClick(View view) {
  23. // get the text from the EditText
  24. EditText etUsername =
  25. (EditText)findViewById(R.id.etUsername);
  26. String stringToPassBack = etUsername.getText().toString();
  27. // put the string to pass back into an intent and close
  28. this activity
  29. Intent myIntent1 = getIntent();
  30. myIntent1.putExtra("@string/user", stringToPassBack);
  31. // get the text
  32. EditText etPassword =
  33. (EditText)findViewById(R.id.etPassword);
  34. String stringToPassBack1 =
  35. etPassword.getText().toString();
  36. // put the string to pass back
  37. Intent myIntent2 = getIntent();
  38. myIntent2.putExtra("@string/pass", stringToPassBack1);
  39. // get the text
  40. EditText etEmail = (EditText)findViewById(R.id.etEmail);
  41. String stringToPassBack2 = etEmail.getText().toString();
  42. // put the string to pass back
  43. Intent myIntent3 = getIntent();
  44. myIntent3.putExtra("@string/email", stringToPassBack2);
  45.  
  46. // Adding the spinner
  47. Spinner spinner1 = (Spinner)findViewById(R.id.spinner);
  48. // Create an ArrayAdapter using the string array and a
  49. default spinner layout
  50. ArrayAdapter<?> adapter =
  51. ArrayAdapter.createFromResource(SecondActivity.this,
  52. R.array.select_length,
  53. android.R.layout.simple_spinner_item);
  54. //Specify the layout to use when the list of choices
  55. appears
  56. adapter.setDropDownViewResource(android.R.layout.
  57. simple_spinner_dropdown_item);
  58. // Apply the adapter to the spinner
  59. spinner1.setAdapter(adapter);
  60.  
  61. String stringToPassBack3 =
  62. spinner1.getSelecetedItem().toString();
  63. Intent myIntent4 = getIntent();
  64. myIntent4.putExtra("@string/spinner_title",
  65. stringToPassBack3);
  66.  
  67. // close activity
  68. setResult(RESULT_OK, myIntent1);
  69. finish();
  70. }
  71. });
  72. }
  73.  
  74. @Override
  75. public void onItemSelected(AdapterView<?> spinner, View view, int pos,
  76. long id) {
  77. spinner = (Spinner)findViewById(R.id.spinner);
  78. spinner.setOnClickListener((View.OnClickListener) this);
  79. String item = spinner.getItemAtPosition(pos).toString();
  80. Toast.makeText(spinner.getContext(), "selected:" + item,
  81. Toast.LENGTH_LONG).show();
  82. }
  83.  
  84.  
  85.  
  86. @Override
  87. public void onNothingSelected(AdapterView<?> spinner) {
  88.  
  89. }
  90. }
  91.  
  92. import android.content.Intent;
  93. import android.support.v7.app.AppCompatActivity;
  94. import android.os.Bundle;
  95. import android.view.View;
  96. import android.widget.Button;
  97. import android.widget.TextView;
  98.  
  99. public class MainActivity extends AppCompatActivity {
  100. private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
  101. Button btn;
  102.  
  103. @Override
  104. protected void onCreate(Bundle savedInstanceState) {
  105. super.onCreate(savedInstanceState);
  106. setContentView(R.layout.activity_main);
  107. btn = (Button)findViewById(R.id.button);
  108. btn.setOnClickListener(new View.OnClickListener() {
  109. @Override
  110. public void onClick(View view) {
  111. Intent myIntent = new Intent(MainActivity.this,
  112. SecondActivity.class);
  113. startActivityForResult(myIntent,
  114. SECOND_ACTIVITY_RESULT_CODE);
  115. }
  116. });
  117. }
  118. // This method is called when the second activity finishes
  119. @Override
  120. protected void onActivityResult(int requestCode, int resultCode,
  121. Intent data) {
  122. super.onActivityResult(requestCode, resultCode, data);
  123. // Check that it is the SecondActivity with an OK result
  124. if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
  125. if (resultCode == RESULT_OK) {
  126. // Get String data from Intent
  127. String etUsername = data.getStringExtra("@string/user");
  128. // Set text view with string
  129. TextView textView = (TextView)findViewById(R.id.textView);
  130. textView.setText(etUsername); {
  131. String etPassword =
  132. data.getStringExtra("@string/pass");
  133. TextView textView1 =
  134. (TextView)findViewById(R.id.textView1);
  135. textView1.setText(etPassword); {
  136. String etEmail =
  137. data.getStringExtra("@string/email");
  138. TextView textView2 =
  139. (TextView)findViewById(R.id.textView2);
  140. textView2.setText(etEmail); {
  141. String spinner =
  142. data.getStringExtra("@string/spinner_title");
  143. TextView textView3 =
  144. (TextView)findViewById(R.id.textView3);
  145. textView3.setText(spinner);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153.  
  154. <Spinner
  155. android:id="@+id/spinner"
  156. android:layout_width="100dp"
  157. android:layout_height="wrap_content"
  158. android:prompt="@string/spinner_title"
  159. android:entries="@array/select_length"
  160. android:spinnerMode="dropdown"
  161. android:layout_toEndOf="@+id/etEmail"
  162. android:layout_marginStart="23dp"
  163. android:layout_below="@+id/etPassword"
  164. android:layout_above="@+id/button1">
  165. </Spinner>
  166.  
  167. <TextView
  168. android:id="@+id/textView3"
  169. android:layout_width="100dp"
  170. android:layout_height="wrap_content"
  171. android:layout_below="@+id/textView1"
  172. android:layout_toEndOf="@+id/text" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement