当我点击按钮时我无法进入下一个活动..但是如果删除比较字符串...意图工作正常..有人可以帮我弄清楚什么是错的吗?
public class MainActivity extends AppCompatActivity {
private Button b1;
private EditText et;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.pass);
s = et.getText().toString();
b1 = (Button)findViewById(R.id.clickhere);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(et.getText().toString().equals(R.string.Ronnie))
{
Intent myIntent = new Intent(MainActivity.this, Thought.class);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
}
}
});
}
}
答案 0 :(得分:2)
if(et.getText().toString().trim().equals(getString(R.string.Ronnie).trim()))
{
Intent myIntent = new Intent(MainActivity.this, Thought.class);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
}
答案 1 :(得分:1)
您正在将int id
与String
进行比较。
if(et.getText().toString().equals(R.string.Ronnie))
{
Intent myIntent = new Intent(MainActivity.this, Thought.class);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
}
将此更改为。
if(et.getText().toString().equals(getString(R.string.Ronnie)))
{
Intent myIntent = new Intent(MainActivity.this, Thought.class);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
}