İçeriğe geç

Android Intent ile Activityler Arasında String Veri Taşımak

Keys;

  • Intent
  • Intent putExtra
  • startActivity
  • Intent getIntent
  • Intent getString
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.intents.MainActivity">
	<EditText
android:id="@+id/editTextMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Give intent text" />
	<Button
android:text="SET INTENT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonMain" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_intent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.intents.IntentActivity">
	<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewIntent" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {

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

    Button buttonMain = (Button) findViewById(R.id.buttonMain);
    final EditText editTextMain = (EditText) findViewById(R.id.editTextMain);

    buttonMain.setOnClickListener(new View.OnClickListener() {@Override
      public void onClick(View view) {

        // Add intent
        Intent intent = new Intent(MainActivity.this, IntentActivity.class);
        //Insert text to activity
        intent.putExtra("INTENT_VALUE", editTextMain.getText().toString());
        // Start activity with intent
        startActivity(intent);

      }
    });
  }
}
public class IntentActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent);

    // Find TextView id
    TextView textViewIntent = (TextView) findViewById(R.id.textViewIntent);
    //Get text intent value
    textViewIntent.setText(getIntent().getExtras().getString("INTENT_VALUE"));
  }
}

Tarih:Android

İlk Yorumu Siz Yapın

Bir Cevap Yazın