出于某种原因,无论我输入我的两个文本框,我都会得到匹配响应.起初我正在使用==,但没有用,所以我尝试切换到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();
}
}
Run Code Online (Sandbox Code Playgroud)
您的edittext没有出现.
使用此代码:
LinearLayout ll = (LinearLayout)findViewById(R.id.linLayout);
Run Code Online (Sandbox Code Playgroud)
linLayout是你的ID LinearLayout中main.xml,取代它不管你有.
ll.setOrientation(LinearLayout.VERTICAL);
numberStudents = new EditText(this);
numberStudents2 = new EditText(this);
ll.addView(numberStudents);
ll.addView(numberStudents2);
Run Code Online (Sandbox Code Playgroud)
或者如果您在XML布局中创建edittext,则使用:
numberStudents = (EditText)findViewById(R.id.et1);
numberStudents2 = (EditText)findViewById(R.id.et2);
Run Code Online (Sandbox Code Playgroud)
比较部分:
if(a.trim().equals(b))
{
// show toast
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |