Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class SecondActivity extends AppCompatActivity implements
- AdapterView.OnItemSelectedListener {
- Button btn2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- btn2 = (Button)findViewById(R.id.button1);
- btn2.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- // get the text from the EditText
- EditText etUsername =
- (EditText)findViewById(R.id.etUsername);
- String stringToPassBack = etUsername.getText().toString();
- // put the string to pass back into an intent and close
- this activity
- Intent myIntent1 = getIntent();
- myIntent1.putExtra("@string/user", stringToPassBack);
- // get the text
- EditText etPassword =
- (EditText)findViewById(R.id.etPassword);
- String stringToPassBack1 =
- etPassword.getText().toString();
- // put the string to pass back
- Intent myIntent2 = getIntent();
- myIntent2.putExtra("@string/pass", stringToPassBack1);
- // get the text
- EditText etEmail = (EditText)findViewById(R.id.etEmail);
- String stringToPassBack2 = etEmail.getText().toString();
- // put the string to pass back
- Intent myIntent3 = getIntent();
- myIntent3.putExtra("@string/email", stringToPassBack2);
- // Adding the spinner
- Spinner spinner1 = (Spinner)findViewById(R.id.spinner);
- // Create an ArrayAdapter using the string array and a
- default spinner layout
- ArrayAdapter<?> adapter =
- ArrayAdapter.createFromResource(SecondActivity.this,
- R.array.select_length,
- android.R.layout.simple_spinner_item);
- //Specify the layout to use when the list of choices
- appears
- adapter.setDropDownViewResource(android.R.layout.
- simple_spinner_dropdown_item);
- // Apply the adapter to the spinner
- spinner1.setAdapter(adapter);
- String stringToPassBack3 =
- spinner1.getSelecetedItem().toString();
- Intent myIntent4 = getIntent();
- myIntent4.putExtra("@string/spinner_title",
- stringToPassBack3);
- // close activity
- setResult(RESULT_OK, myIntent1);
- finish();
- }
- });
- }
- @Override
- public void onItemSelected(AdapterView<?> spinner, View view, int pos,
- long id) {
- spinner = (Spinner)findViewById(R.id.spinner);
- spinner.setOnClickListener((View.OnClickListener) this);
- String item = spinner.getItemAtPosition(pos).toString();
- Toast.makeText(spinner.getContext(), "selected:" + item,
- Toast.LENGTH_LONG).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> spinner) {
- }
- }
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
- Button btn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn = (Button)findViewById(R.id.button);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent myIntent = new Intent(MainActivity.this,
- SecondActivity.class);
- startActivityForResult(myIntent,
- SECOND_ACTIVITY_RESULT_CODE);
- }
- });
- }
- // This method is called when the second activity finishes
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- // Check that it is the SecondActivity with an OK result
- if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
- if (resultCode == RESULT_OK) {
- // Get String data from Intent
- String etUsername = data.getStringExtra("@string/user");
- // Set text view with string
- TextView textView = (TextView)findViewById(R.id.textView);
- textView.setText(etUsername); {
- String etPassword =
- data.getStringExtra("@string/pass");
- TextView textView1 =
- (TextView)findViewById(R.id.textView1);
- textView1.setText(etPassword); {
- String etEmail =
- data.getStringExtra("@string/email");
- TextView textView2 =
- (TextView)findViewById(R.id.textView2);
- textView2.setText(etEmail); {
- String spinner =
- data.getStringExtra("@string/spinner_title");
- TextView textView3 =
- (TextView)findViewById(R.id.textView3);
- textView3.setText(spinner);
- }
- }
- }
- }
- }
- }
- }
- <Spinner
- android:id="@+id/spinner"
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:prompt="@string/spinner_title"
- android:entries="@array/select_length"
- android:spinnerMode="dropdown"
- android:layout_toEndOf="@+id/etEmail"
- android:layout_marginStart="23dp"
- android:layout_below="@+id/etPassword"
- android:layout_above="@+id/button1">
- </Spinner>
- <TextView
- android:id="@+id/textView3"
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:layout_below="@+id/textView1"
- android:layout_toEndOf="@+id/text" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement