我是新來的,也是在android開發中。我的代碼有問題 我希望當我點擊收音機組時,必須更改文本視圖的文本。但這沒有發生。有人可以幫我 這裏是我的代碼在android中更改textview的值
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;
QuerryClass qc=new QuerryClass(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);
}
}
是不是真的你的意圖,你的隨機randm將是百達相同的價值? – Opiatefuchs
你不應該註冊的radiogroup checkchangedlistener 2次......這是第一個錯誤..我認爲這就是爲什麼在事件調用...第一個監聽器被解僱......並沒有什麼可以在textview上工作..作爲監聽器塊不包含代碼來對textview文本進行更改 –
先生檢查我上傳的新代碼 但它也不起作用 –