在按钮的onClick(View v)中没有调用if条件。我该怎么办?
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
String k;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText j = (EditText) findViewById(R.id.editText);
}
public void nih(View v) {
EditText j = (EditText) findViewById(R.id.editText);
if (j.equals("hello")) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("nishan", "london");
startActivity(intent);
}
}
答案 0 :(得分:0)
您需要获取EditText (j)
的文本值,如下所示:
j.getText().toString().equals("hello")
除此之外,最好不要回忆findViewById
来再次检索这个EditText。您已经在onCreate
中拥有它,因此,您的代码应为:
EditText j; // make a global var
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
j = (EditText) findViewById(R.id.editText); // get its id
}
public void nih(View v) {
if (j.getText().toString().equals("hello")) { // use it
...
}
}