Android - Working with Radio Groups, have seven radio buttons, just need value from 0-6
I'm really new to Android and am trying to do something I think should be simple.
I have two activities, a MainActivity and an intent. The intent is basically a set of 7 radio buttons and an OK and cancel button.
I'm trying to send a value (from 0-6 or 1-7) from MainActivity to set the checked radio button in the radio group and then return a value (from 0-6 or 1-7) back to the MainActivity depending on which option it selected in the radio group. As far as I can tell getCheckedRadioButtonId() is not returning what I need.
Here's my code so far.
MainActivity:
package com.android.phil.buttontest;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;
public class MainActivity extends Activity { protected static final int SUB_ACTIVITY_REQUEST_CODE = 1337; int maType = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.setMA);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent(MainActivity.this, maTypeActivity.class);
Bundle b = new Bundle();
b.putInt("maType", maType);
intent.putExtras(b);
startActivityForResult(intent,SUB_ACTIVITY_REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data );
// Here We identify the subActivity we started
if(requestCode == SUB_ACTIVITY_REQUEST_CODE)
{
Bundle extras = data.getExtras();
maType = extras.getInt("maType");
Toast.makeText(this, "The data returned is: "+maType, Toast.LENGTH_SHORT).show();
}
}
}
SubActivity
package com.android.phil.buttontest;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.Toast;
public class maTypeActivity extends Activity { RadioGroup radioGroup1; Button ok; Button cancel;
int maType = 0;
protected final int RESULT_OK = 1;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.ma_type);
Bundle b = this.getIntent().getExtras();
maType = b.getInt("maType");
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup1.check(maType)开发者_StackOverflow社区;
Button ok = (Button) findViewById(R.id.okButton);
Button cancel = (Button) findViewById(R.id.cancelButton);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
maType = radioGroup1.getCheckedRadioButtonId();
Intent data = new Intent();
data.putExtra("MAType",maType);
maTypeActivity.this.setResult(RESULT_OK, data);
maTypeActivity.this.finish();
}
});
cancel.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
maTypeActivity.this.finish();
}
});
}
}
I have sorted this out if you want to know the answer let me know and I can provide you with some details.
Phil
精彩评论