0

我的输出为零。我找不到任何错误

任何帮助都会很棒。

完整代码:

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

4 回答 4

2

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

b1、b2、b3 被初始化为默认值,即“0”,因此结果始终为“0”

将所有 getText 代码写入bfour.setOnClickListener()

或者实现 TextWatcher 并使用 onTextChanged 方法

于 2012-08-16T05:04:14.767 回答
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));
            }
        });
    }
}
于 2012-08-16T04:58:36.317 回答
1

利用

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

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

最后输入字符串时是否有空格?一旦检查您作为输入提供的字符串的长度是否准确

于 2012-08-16T04:48:57.643 回答
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);

        }
    });
}
于 2012-08-16T05:29:35.027 回答