Str.length()不工作,因为我认为它应该



当我输入"AA"进入我的2 TextViews它永远不会移动到我的其他活动时,"AA".length()应该是2使它切换到我的其他活动,但它从来没有。然后我检查了一下它打印出来的是什么,它打印的是98和94作为字符串的两个长度,而它应该是22,有什么建议吗?

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

}

try

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

我认为你没有正确地从TextView中获取文本。

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

TextView.toString()返回对象的可读描述,而不是输入的实际文本(来自API文档)。

相关内容

最新更新