2014-03-13 94 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)。

相关问题