I have code with three buttons. I want to assign, I believe, onClickListener to each of them. So that when each respective button is clicked, I get a value I can save and use in other activities.
Eventually I will use this value, to compare to another value selected via another activity, and depending on the matches (the two values), each will be assigned a random number between 1-6 and others.
Here is my .XML below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.cc09june.TeamAOffense">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Team A Offense Selection"
android:id="@+id/TeamAOffSel"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PandR"
android:id="@+id/PandRA"
android:layout_marginTop="32dp"
android:layout_below="@+id/TeamAOffSel"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="exchange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MandF"
android:id="@+id/MandFA"
android:layout_below="@+id/PandRA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="exchange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SandC"
android:id="@+id/SandCA"
android:layout_below="@+id/MandFA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="exchange" />
Here is my .java below:
package com.example.android.cc09june;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class TeamAOffense extends ActionBarActivity {
public void exchange (View view){
//need to make it link to TeamA.xml
Intent intent = new Intent(TeamAOffense.this, ExchangeAToB.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_aoffense);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_team_aoffense, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} }