结果为零(android)

时间:2012-08-16 04:29:51

标签: android textview

输出为零。我在其中找不到任何错误

任何帮助都会很棒。

完整代码:

package com.equbez.resistor_decoder;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Second extends Activity {
    int i,b1,b2,b3,result,four;
    String one,two,three; 
    String arr[]={"black","brown","red","orange","yellow","green","blue","violet","grey","white"};

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        EditText etone=(EditText) findViewById(R.id.editText1);
        one=etone.getText().toString();

        for(i=0;i<9;i++) {
            if(one.equalsIgnoreCase(arr[i])) {
                b1=i;
            }
        }

        EditText ettwo=(EditText) findViewById(R.id.editText2);
        two=ettwo.getText().toString();

        for(i=0;i<9;i++) {
            if(two.equalsIgnoreCase(arr[i])) {
                b2=i;
            }
        }

        EditText etthree=(EditText) findViewById(R.id.editText3);
        three=etthree.getText().toString();

        for(i=0;i<9;i++) {
            if(three.equalsIgnoreCase(arr[i])) {
                b3=i;
            }
        }

        result=b2+b2+b3;
        Button bfour=(Button) findViewById(R.id.button4);
        bfour.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View V) {
                TextView tv=(TextView) findViewById(R.id.textView4);    
                tv.setText(""+result);
            }
        });
    }
}

任何帮助都会很棒。

4 个答案:

答案 0 :(得分:2)

最初,当调用活动时,EditTextView将为空。所以你总是得到一个空字符串,你要与arr []中的字符串进行比较。

b1,b2,b3被初始化为默认值,即。,,&#39; 0&#39;因此结果始终是&#39; 0&#39;

bfour.setOnClickListener()

中写下所有getText代码

或者实现TextWatcher并使用onTextChanged方法

答案 1 :(得分:1)

使用

for(i = 0 ; i< arr.length ;i++)
do something

在您的代码中,如果您输入白色,则不进行比较

你在最后输入字符串时是否给出任何空格?一旦检查你输入的字符串的长度是否确切

答案 2 :(得分:1)

这里我已经重新编码了这些代码,

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Second extends Activity 
{
    private static int b1,b2,b3,result,four;
    private String one,two,three; 
    private String arr[]={"black","brown","red","orange","yellow","green","blue","violet","grey","white"};

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        EditText etone=(EditText) findViewById(R.id.editText1);

        one=etone.getText().toString();
        for( int i = 0 ; i < 10; i++ )
        {
            if(one.trim().equalsIgnoreCase(arr[i].trim()))
            {
                b1=i;
                break;
            }
        }

        EditText ettwo=(EditText) findViewById(R.id.editText2);
        two=ettwo.getText().toString();

        for( int i = 0 ; i < 10; i++ )
        {

            if(two.trim().equalsIgnoreCase(arr[i].trim()))
            {
                b2=i;
                break;
            }
        }

        EditText etthree=(EditText) findViewById(R.id.editText3);
        three=etthree.getText().toString();

        for( int i = 0 ; i < 10; i++ )
        {
            if(three.trim().equalsIgnoreCase(arr[i].trim()))
            {
                b3=i;
                break;
            }
        }

        result=b2+b2+b3;
        Button bfour=(Button) findViewById(R.id.button4);
        bfour.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View V) 
            {
                TextView tv=(TextView) findViewById(R.id.textView4);    
                tv.setText( String.valueOf(result));
            }
        });
    }
}

答案 3 :(得分:0)

我遇到了问题。单击按钮后,您必须运行所有3个循环。你已经在Oncreate()上做到了。所以你总是得到0。 见下文:

int i, b1, b2, b3, result, four;
String one, two, three;
String arr[] = { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" };
EditText etone, ettwo, etthree;
Button bfour;
TextView tv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    etone = (EditText) findViewById(R.id.editText1);
    ettwo = (EditText) findViewById(R.id.editText2);
    etthree = (EditText) findViewById(R.id.editText3);
    bfour = (Button) findViewById(R.id.button4);
    bfour.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View V) {

            one = etone.getText().toString();

            for (i = 0; i < arr.length; i++) {
                if (one.equals(arr[i])) {
                    b1 = i;
                    System.out.println("dhrupal one="+b1);
                }
            }
            two = ettwo.getText().toString();

            for (i = 0; i < arr.length; i++) {

                if (two.equals(arr[i])) {
                    System.out.println("dhrupal one="+two);
                    b2 = i;
                }
            }
            three = etthree.getText().toString();

            for (i = 0; i < arr.length; i++) {
                if (three.equals(arr[i])) {
                    System.out.println("dhrupal one="+three);
                    b3 = i;
                }
            }

            result = b1 + b2 + b3;
            tv = (TextView) findViewById(R.id.textView4);
            tv.setText("result=" + result);

        }
    });
}