2015-11-01 197 views
-1

我創建了一個登錄表單並希望檢查用戶憑據是正確還是錯誤。 但在我發現之前,我無法獲得字符串變量中的editText值。 所以請幫我存放editText的值。無法從editText獲取數據android studio

我的Java代碼

private String output; 
private Toolbar toolbar; 
private Button button; 
public EditText Username,Password; 
public String myURL,userValue23,passValue; 

List<DataModel> loginList; 

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

    toolbar = (Toolbar) findViewById(R.id.app_bar); 
    setSupportActionBar(toolbar); 

    Username = (EditText) findViewById(R.id.etUsername); 
    Password = (EditText) findViewById(R.id.etPassword); 

    userValue23 = Username.getText().toString(); 
    passValue = Password.getText().toString(); 

    button = (Button) findViewById(R.id.btn_login); 
    button.setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 

    if(v.getId()==R.id.btn_login) 
    { 
     if(inOnline()) 
     { 
      if(userValue=="") 
      { 
       Toast.makeText(this, "tees", Toast.LENGTH_LONG).show(); 
      }else{ 
       Toast.makeText(this, userValue+"tees", Toast.LENGTH_LONG).show(); 
      } 
     } 
     else 
     { 
      Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

在XML代碼中,我創建的登錄表單有兩個EDITTEXT的形式,而這些ID是分別etUsername,etPassword,btn_login

回答

0
 if(inOnline()) 
      { 
       if(userValue=="") 
       { 
        Toast.makeText(this, "tees", Toast.LENGTH_LONG).show(); 
       }else{ 

**您使用值這個名字是userValue但你沒有定義它,但你定義它,你定義userValue23 ..修復它並不需要你varibale移動到的onclick功能 **

    Toast.makeText(this, userValue+"tees", Toast.LENGTH_LONG).show(); 
       } 
      } 
1

把這些

userValue23 = Username.getText().toString(); 
passValue = Password.getText().toString(); 

內onClick方法

public void onClick(View v) { 

if(v.getId()==R.id.btn_login) 
{ 
    userValue23 = Username.getText().toString(); 
    passValue = Password.getText().toString(); 
    if(inOnline()) 
    { 
     if(userValue23=="") 
     { 
      Toast.makeText(this, "tees", Toast.LENGTH_LONG).show(); 
     }else{ 
      Toast.makeText(this, userValue23+"tees", Toast.LENGTH_LONG).show(); 
     } 
    } 
    else 
    { 
     Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show(); 
    } 
} 
} 
0

您需要這兩行

userValue23 = Username.getText().toString(); 
passValue = Password.getText().toString(); 

移動OnClick方法內。否則,他們將永遠是空的OnCreate