[英]Why can't I compare these 2 strings?
出於某種原因,無論我在兩個文本框中鍵入什么內容,我都會得到匹配的響應。 起初我使用的是==
,但是沒有用,所以我嘗試切換到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();
}
}
您的edittext沒有出現。
使用此代碼:
LinearLayout ll = (LinearLayout)findViewById(R.id.linLayout);
linLayout
是main.xml
LinearLayout
的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
}
看一下這段代碼。 這是您必須檢查提交或單擊按鈕時從編輯文本中獲取的字符串的方法
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();
}
}
});
}
}
您尚未聲明要使用哪個editText,基本上是將兩個具有相同數據但名稱不同的String對象進行比較,這就是其始終顯示匹配的原因。
1.轉到main.xml
2.創建到editText,例如editText1和editText2
3.現在在您的代碼中使用它們如下
numberStudents = (EditText)findViewById(R.id.editTex1);
numberStudents2 = (EditText)findViewById(R.id.editTex2);
現在應該可以了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.