2015-04-30 206 views
-1

我是新來的,也是在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); 
 
    } 
 
}

+0

是不是真的你的意圖,你的隨機randm將是百達相同的價值? – Opiatefuchs

+0

你不應該註冊的radiogroup checkchangedlistener 2次......這是第一個錯誤..我認爲這就是爲什麼在事件調用...第一個監聽器被解僱......並沒有什麼可以在textview上工作..作爲監聽器塊不包含代碼來對textview文本進行更改 –

+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; 
    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); 
     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); 
    } 
} 
+0

先生我使用了您的建議但我沒有收到我的結果 我上傳了新的我認爲更好的變化 –

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(); 
      } 


     }); 
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 

      } 

     } 



    }); 
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; 
 
    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((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); 
 
    } 
 
}