在Java中比较字符串 [英] Comparing strings in Java
问题描述
我试着去比较两个的EditText框的值。我想是只比较passw1 = passw2。正如我的code现在比较两个字符串我已经进入了,因为我无法去比较它们。
最后的EditText passw1 =(EditText上)findViewById(R.id.passw1);
最后的EditText passw2 =(EditText上)findViewById(R.id.passw2);
按钮buttoks =(按钮)findViewById(R.id.Ok);
buttoks.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(passw1.toString()equalsIgnoreCase(1234)及。&安培; passw2.toString()equalsIgnoreCase(1234)。){
Toast.makeText(getApplication()的用户名和密码匹配,Toast.LENGTH_SHORT).show();
}
其他 {
Toast.makeText(getApplication()的用户名和密码不匹配,Toast.LENGTH_SHORT).show();
}
}});
我刚才犯了一个错误,因为,为了获取文本,你需要使用.getText()的toString()。
下面是一个完整的工作的例子:
包com.psegina.passwordTest01;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;
进口android.widget.Toast;
公共类主要活动扩展实现OnClickListener {
的LinearLayout升;
的EditText用户;
的EditText PWD;
按钮BTN;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
L =新的LinearLayout(本);
用户=新的EditText(本);
PWD =新的EditText(本);
BTN =新的按钮(这一点);
l.addView(用户);
l.addView(PWD);
l.addView(BTN);
btn.setOnClickListener(本);
的setContentView(升);
}
公共无效的onClick(视图v){
。字符串U = user.getText()的toString();
。串P = pwd.getText()的toString();
如果(u.equals(P))
Toast.makeText(getApplicationContext(),匹配,Toast.LENGTH_SHORT).show();
其他
Toast.makeText(getApplicationContext(),user.getText()+ pwd.getText(),Toast.LENGTH_SHORT=!).show();
}
}
原来的答复(因为缺乏的toString()将无法正常工作)
尝试使用.getText(),而不是的ToString()。
如果(passw1.getText()== passw2.getText())
#干点什么
的ToString()返回一个字符串再对整个对象的presentation,这意味着它不会返回您在字段中输入的文本(看看自己加入了吐司,它会显示的ToString输出( ))
Im trying to compare the values of two edittext boxes. What i would like is to just compare passw1 = passw2. As my code is now comparing two strings i have entered as i could not get to compare them.
final EditText passw1= (EditText) findViewById(R.id.passw1);
final EditText passw2= (EditText) findViewById(R.id.passw2);
Button buttoks = (Button) findViewById(R.id.Ok);
buttoks.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (passw1.toString().equalsIgnoreCase("1234") && passw2.toString().equalsIgnoreCase("1234")){
Toast.makeText(getApplication(),"Username and password match", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplication(),"Username and password doesn't match", Toast.LENGTH_SHORT).show();
}
} });
[EDIT] I made a mistake earlier, because, to get the text, you need to use .getText().toString().
Here is a full working example:
package com.psegina.passwordTest01;
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.LinearLayout;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
LinearLayout l;
EditText user;
EditText pwd;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
l = new LinearLayout(this);
user = new EditText(this);
pwd = new EditText(this);
btn = new Button(this);
l.addView(user);
l.addView(pwd);
l.addView(btn);
btn.setOnClickListener(this);
setContentView(l);
}
public void onClick(View v){
String u = user.getText().toString();
String p = pwd.getText().toString();
if( u.equals( p ) )
Toast.makeText(getApplicationContext(), "Matches", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), user.getText()+" != "+pwd.getText(), Toast.LENGTH_SHORT).show();
}
}
Original answer (Will not work because of the lack of toString())
Try using .getText() instead of .toString().
if( passw1.getText() == passw2.getText() )
#do something
.toString() returns a String representation of the whole object, meaning it won't return the text you entered in the field (see for yourself by adding a Toast which will show the output of .toString())
这篇关于在Java中比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!