2014-03-13 92 views
-1

當我輸入「AA」到我的2 TextViews它永遠不會移動到我的其他acitivty時,「AA」.length()應該是2使它切換到我的其他活動但它從來沒有。然後我檢查了看它打印出來的結果是什麼,它應該是2 2的任何建議,打印98和94作爲2個字符串的長度?str.length()不工作,因爲我認爲它應該

public class DataEntry extends Activity { 
String parent1 = ""; 
String parent2 = ""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_data_entry); 
    Button calc = (Button)findViewById(R.id.btnCalc); 

    calc.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v){ 
      TextView tv = (TextView)findViewById(R.id.txtFirstParent); 
      TextView tv1 = (TextView)findViewById(R.id.txtParent2); 
      String str = tv.toString().trim(); 
      String str2 = tv1.toString().trim(); 
      parent1 = str; 
      parent2 = str2; 
      if(str.length()==2 && str2.length()==2){ 
       Intent otherIntent = new Intent(DataEntry.this, MonoHybrid.class); 
       otherIntent.putExtra("parent1",parent1); 
       otherIntent.putExtra("parent2",parent2); 
       startActivity(otherIntent); 
      } 
      else if(str.length()==4 && str2.length()==4){ 
       Intent otherIntent = new Intent(DataEntry.this, DiHybrid.class); 
       otherIntent.putExtra("parent1",parent1); 
       otherIntent.putExtra("parent2",parent2); 
       startActivity(otherIntent); 
      } 
      else if(str.length()==6 && str2.length()==6){ 
       Intent otherIntent = new Intent(DataEntry.this, TriHybrid.class); 
       otherIntent.putExtra("parent1",parent1); 
       otherIntent.putExtra("parent2",parent2); 
       startActivity(otherIntent); 
      } 
      else 
       tv.setText(str.length() +" "+ str2.length()); 
       tv1.setText(str.length() + " " + str2.length()); 
       //tv.setText("Error please enter a MonoHybrid \"AA\" DiHybrid \"AABB\" or TriHybrid \"AABBCC\""); 
       //tv1.setText("Error please enter a MonoHybrid \"AA\" DiHybrid \"AABB\" or TriHybrid \"AABBCC\""); 
     } 
    }); 
} 

}

回答

2

嘗試

String str = tv.getText().toString().trim(); 

代替。

0

我不認爲你正確地從TextView中獲取文本。

嘗試

String str = tv.getText().toString(); 
String str2 = tv1.getText().toString(); 

TextView.toString()返回已輸入的對象,而不是實際的文本的人類可讀的描述(從API documentation)。

相關問題