为什么我不能比较这两个字符串?

时间:2013-03-18 10:07:10

标签: java android

出于某种原因,无论我输入两个文本框,我都会得到匹配响应。 起初我使用==但是没有用,所以我尝试切换到if( a.equals( b ))

我仍然被困住了。

请帮忙!

package net.2TextboxesStringCompare;

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

/*
 * 2TextboxesStringCompare
 */


public class Code8 extends Activity implements OnClickListener {

    Button accept;
    EditText numberStudents, numberStudents2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        numberStudents = new EditText(this);
        numberStudents2 = new EditText(this);

        // find our button in the UI by its ID
        accept = (Button)findViewById(R.id.accept);

        // set listeners for the button. Since our current class
        // implements the OnClickListener interface
        // we can simply pass the current object ("this") into
        // the listener. The appropriate method will therefore
        // be called when the event fires.
        accept.setOnClickListener(this);

    }

    /* implement an event handler that responds to click events */
    public void onClick(View v){
        String a = numberStudents.getText().toString();
        String b = numberStudents2.getText().toString();
        if( a.equals( b ) )
            Toast.makeText(getApplicationContext(), "Matches", 
            Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(getApplicationContext(), numberStudents.getText()+" !=      
            "+numberStudents2.getText(), Toast.LENGTH_SHORT).show();
    }

}

3 个答案:

答案 0 :(得分:6)

您的编辑文字没有显示。

使用此代码:

LinearLayout ll = (LinearLayout)findViewById(R.id.linLayout);

linLayoutLinearLayoutmain.xml的ID,无论您拥有什么,都可以替换它。

ll.setOrientation(LinearLayout.VERTICAL);

 numberStudents = new EditText(this);
 numberStudents2 = new EditText(this);

ll.addView(numberStudents);
ll.addView(numberStudents2);

如果您在XML布局中创建edittext,请使用:

numberStudents = (EditText)findViewById(R.id.et1);
numberStudents2  = (EditText)findViewById(R.id.et2);

比较部分:

if(a.trim().equals(b))
{
     // show toast
}

答案 1 :(得分:0)

看看这段代码。这是你必须检查在提交或按钮点击时从编辑文本中获得的字符串的方式

package com.example.testr;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button button1;
    EditText editText1, editText2;
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context=this;
        button1=(Button)findViewById(R.id.button1);

        editText1=(EditText)findViewById(R.id.editText1);
        editText2=(EditText)findViewById(R.id.editText2);



        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub



                if ((editText1.getText().toString().trim()).equals(editText2.getText().toString().trim())) {

                    Toast.makeText(context, "both are equal", 1000).show();
                }
                else
                {
                    Toast.makeText(context, "both are not  equal", 1000).show();
                }
            }
        });




    }



}

答案 2 :(得分:0)

你没有声明要使用哪个editText,基本上你要比较两个具有相同数据但不同名称的String对象,这就是为什么它始终显示匹配。为了纠正这个

1.转到main.xml

2.创建editText,比如editText1和editText2

3.现在在你的代码中使用它们如下

numberStudents = (EditText)findViewById(R.id.editTex1);
numberStudents2  = (EditText)findViewById(R.id.editTex2);

现在应该没问题。