想要将人名带到下一个活动,如果登录不正确,这将显示吐司不起作用,只显示吐司。应该在下一个活动中添加什么,这个代码有什么问题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText eText1 = (EditText)findViewById(R.id.editText1);
final EditText eText2 = (EditText)findViewById(R.id.editText2);
final Button btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener( new View.OnClickListener(){
public void onClick(View v) {
String str = eText1.getText().toString();
String log= "malaika" ;
if(str == log){
Intent intent=new Intent(getApplicationContext(),FirstButton.class);
intent.putExtra("welcome", log);
}
else {
Toast tos =Toast.makeText(getApplication(),"wrong",Toast.LENGTH_SHORT);
tos.show();
}
}});
答案 0 :(得分:0)
首先在equals
的地方使用==
并开始新的活动
if(str.equals(log))
{
Intent intent=new Intent(getApplicationContext(),FirstButton.class);
intent.putExtra("welcome", log);
/*'welcome' is acting as a key which will carry your
name from this activity to the FirstButton ACTIVITY
I suggest changing it
*/
startActivity(intent);//for starting new activity
}
else
{
Toast tos =Toast.makeText(getApplication(),"wrong",Toast.LENGTH_SHORT);
tos.show();
}
以及您可以通过
获取姓名的FirstButton活动Intent i=getIntent();
String name=i.getStringExtra("welcome");