2014-10-10 51 views
2

想要將人物名稱帶到下一個活動中,並且如果登錄不正確,這會顯示吐司不起作用並且只顯示吐司。那麼應該在下一步添加什麼?活動,什麼是錯,此代碼我想在主要活動中添加意圖

 @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

第一對==的地方全部使用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"); 
+0

請告訴我如何顯示下一頁上的名稱應該使用文本字段?或圖片瀏覽請建議 – 2014-10-10 06:20:49

+0

你應該使用** Textview ** – nobalG 2014-10-10 08:12:01