我是新来的,也是android开发的新手。我的代码有问题 我希望当我点击广播组时,必须更改文本视图的文本。但这不会发生。有人可以帮帮我吗?
这是我的代码
package com.example.mine4.truefalse;
import android.app.Activity;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class NewGame extends Activity {
int min = 1;
int max = 20;
int randm = (int)(Math.random() * max) + min;
TextView level;
TextView clock,text;
RadioGroup rg1;
int pos1,pos2,AVal,j=0;
RadioButton radio1,radio2;
QueryClass qc = new QueryClass(this);
int correct=0;
int wrong=0;
String q;
ArrayList QVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
text = (TextView) findViewById(R.id.textv);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
rg1 = (RadioGroup) findViewById(R.id.rg1);
qc.open();
AVal = qc.getANSData(randm);
QVal = qc.getQueData(randm);
q = (String) QVal.get(j);
text.setText(q);
qc.close();
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
pos1 = rg1.indexOfChild(findViewById(checkedId));
pos2 = rg1.indexOfChild(findViewById(rg1.getCheckedRadioButtonId()));
}
});
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
pos1 = rg1.indexOfChild(findViewById(checkedId));
pos2 = rg1.indexOfChild(findViewById(rg1.getCheckedRadioButtonId()));
if(pos2==AVal){
correct++;
text.setText("The fastest time to eat 15 Ferrero Rocher is 1 minute 10 seconds.");
}
else if(pos2!=AVal) {
text.setText("The fastest time to eat 15 Ferrero Rocher is 1 minute 10 seconds.");
wrong++;
// Toast.makeText(NewGame.this,wrong+"",Toast.LENGTH_LONG).show();
}
Toast.makeText(NewGame.this,pos2+"",Toast.LENGTH_SHORT).show();
Check();
}
});
}
public void Check(){
if(pos2==AVal){
correct++;
//Toast.makeText(NewGame.this,correct+"",Toast.LENGTH_LONG).show();
}
else if(pos2!=AVal) {
wrong++;
// Toast.makeText(NewGame.this,wrong+"",Toast.LENGTH_LONG).show();
}
}
@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_new_game, 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);
}
@Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
Intent intn = new Intent(NewGame.this,Exmain.class);
startActivity(intn);
}
}
答案 0 :(得分:0)
我已经更改了你的代码..现在检查
package com.example.mine4.truefalse;
import android.app.Activity;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class NewGame extends Activity {
int min = 1;
int max = 20;
int randm = (int)(Math.random() * max) + min;
TextView level;
Button b1;
TextView text;
RadioGroup rg1;
int pos1=0,pos2,AVal,j=0;
RadioButton radio1,radio2;
QueryClass qc = new QueryClass(this);
int correct=0;
int wrong=0;
String q;
ArrayList QVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
text = (TextView) findViewById(R.id.textv);
b1=(Button)findViewById(R.id.b1);
// radio1 = (RadioButton) findViewById(R.id.radio1);
// radio2 = (RadioButton) findViewById(R.id.radio2);
// rg1 = (RadioGroup) findViewById(R.id.rg1);
qc.open();
AVal = qc.getANSData(randm);
QVal = qc.getQueData(randm);
q = (String) QVal.get(j);
text.setText(q);
qc.close();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(j=0;j<QVal.size();j++) {
if(pos1<9) {
text.setText((String) QVal.get(j));
pos1++;
}
}
}
});
}
public void Check(){
if(pos2==AVal){
correct++;
//Toast.makeText(NewGame.this,correct+"",Toast.LENGTH_LONG).show();
}
else if(pos2!=AVal) {
wrong++;
// Toast.makeText(NewGame.this,wrong+"",Toast.LENGTH_LONG).show();
}
}
@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_new_game, 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);
}
@Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
Intent intn = new Intent(NewGame.this,Exmain.class);
startActivity(intn);
}
}
答案 1 :(得分:0)
尝试这样的事情:
RadioGroup rg1=(RadioGroup) findViewById(R.id.rg1);
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(radio1.isChecked())
{
text.setText("The fastest time to eat 15 Ferrero Rocher is 1 minute 10 seconds."); //or what you want to stay inyour textView
}
else if(radio2.isChecked())
{
text.setText("The fastest time to eat 15 Ferrero Rocher is 1 minute 10 seconds."); //or what you want to stay inyour textView
}
}
});
而不是代码的这一部分:
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
pos1 = rg1.indexOfChild(findViewById(checkedId));
pos2 = rg1.indexOfChild(findViewById(rg1.getCheckedRadioButtonId()));
}
});
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
pos1 = rg1.indexOfChild(findViewById(checkedId));
pos2 = rg1.indexOfChild(findViewById(rg1.getCheckedRadioButtonId()));
if(pos2==AVal){
correct++;
text.setText("The fastest time to eat 15 Ferrero Rocher is 1 minute 10 seconds.");
}
else if(pos2!=AVal) {
text.setText("The fastest time to eat 15 Ferrero Rocher is 1 minute 10 seconds.");
wrong++;
// Toast.makeText(NewGame.this,wrong+"",Toast.LENGTH_LONG).show();
}
Toast.makeText(NewGame.this,pos2+"",Toast.LENGTH_SHORT).show();
Check();
}
});
答案 2 :(得分:0)
试试这个..
RadioGroup rg1;
TextView text;
text = (TextView) findViewById(R.id.textv);
rg1= (RadioGroup) findViewById(R.id.rg1);
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.radio1) {
text.setText("Radio 1 selected");
Toast.makeText(getApplicationContext(), "Radio 1 selected",Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.radio2) {
text.setText("Radio 2 selected");
Toast.makeText(getApplicationContext(), "Radio 2 selected",Toast.LENGTH_SHORT).show();
} else {
//do something when nothing selected
}
}
});
答案 3 :(得分:0)
这是新代码,请查看
package com.example.mine4.truefalse;
import android.app.Activity;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class NewGame extends Activity {
int min = 1;
int max = 20;
int randm = (int)(Math.random() * max) + min;
TextView level;
TextView clock,text;
RadioGroup rg1;
int pos1,pos2,AVal,j=0,k=0;
RadioButton radio1,radio2;
QueryClass qc = new QueryClass(this);
int correct=0;
int wrong=0;
String q;
ArrayList QVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
text = (TextView) findViewById(R.id.textv);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
rg1 = (RadioGroup) findViewById(R.id.rg1);
qc.open();
AVal = qc.getANSData(randm);
QVal = qc.getQueData(randm);
q = (String) QVal.get(j);
text.setText((String) QVal.get(j));
qc.close();
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(j=0;j<QVal.size();j++) {
if(k<9) {
text.setText((String) QVal.get(j));
pos1 = rg1.indexOfChild(findViewById(checkedId));
pos2 = rg1.indexOfChild(findViewById(rg1.getCheckedRadioButtonId()));
Check();
k++;
}
}
}
});
}
public void Check(){
if(pos2==AVal){
correct++;
//Toast.makeText(NewGame.this,correct+"",Toast.LENGTH_LONG).show();
}
else if(pos2!=AVal) {
wrong++;
// Toast.makeText(NewGame.this,wrong+"",Toast.LENGTH_LONG).show();
}
}
@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_new_game, 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);
}
@Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
Intent intn = new Intent(NewGame.this,Exmain.class);
startActivity(intn);
}
}
&#13;