1

出于某种原因,无论我在两个文本框中输入什么内容,都会得到匹配响应。一开始我在使用==,但没有用,所以我尝试切换到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();
    }

}
4

3 回答 3

6

您的编辑文本没有出现。

使用此代码:

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

linLayout是您的LinearLayoutin的 id main.xml,无论您拥有什么,都可以替换它。

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
}
于 2013-03-18T10:21:17.300 回答
0

您还没有声明要使用哪个editText,基本上您正在比较两个具有相同数据但名称不同的字符串对象,这就是它总是显示匹配的原因。纠正这个

1.转到main.xml

2.create to editText,比如editText1和editText2

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

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

现在应该没事了。。

于 2013-03-18T10:40:43.460 回答
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();
                }
            }
        });




    }



}
于 2013-03-18T10:32:26.297 回答