2016-10-03 77 views
1

據我所知,這將存儲用戶並傳入我所創建的變量。然後獲取文本並將其轉換爲字符串。現在如果發生這種情況,當我打電話時只有一個空白的氣泡出現?爲什麼當我有一個字符串時,吐司輸出中會出現空白文本?

package com.set.ultimax.login; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
public class Main extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button logIn; 
    EditText user; 
    EditText pass; 

    logIn = (Button) findViewById(R.id.btn); 
    user = (EditText) findViewById(R.id.username); 
    pass = (EditText) findViewById(R.id.password); 

    final String userValue = user.getText().toString(); 
    final String passValue = pass.getText().toString(); 

    logIn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
      Toast.makeText(Main.this,userValue, Toast.LENGTH_LONG).show(); 
      Toast.makeText(Main.this,passValue, Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 
} 
+0

請接受回答 – nandsito

回答

1

你必須得到來自EditText s的時間文本單擊而不是得到它onCreate按鈕。

試試這個:

logIn.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // Perform action on click 

     final String userValue = user.getText().toString(); 
     final String passValue = pass.getText().toString(); 

     Toast.makeText(Main.this,userValue, Toast.LENGTH_LONG).show(); 
     Toast.makeText(Main.this,passValue, Toast.LENGTH_LONG).show(); 
    } 
}); 
+0

非常感謝。 –

1

別拿值的onCreate,做在聽衆和不顯示兩個連續祝酒詞在他們會重疊。類似這樣的:

logIn.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // Perform action on click 
     EditText user = (EditText) findViewById(R.id.username); 
     EditText pass = (EditText) findViewById(R.id.password); 

     String userValue = user.getText().toString(); 
     String passValue = pass.getText().toString(); 

     Toast.makeText(Main.this,"User: " + userValue + " Pass: " + passValue, Toast.LENGTH_LONG).show(); 
    } 
}); 
+0

謝謝你。學過的知識。 –