我有一个fragment命名fragment_login.xml和相应的类命名LoginPageFragment.java。在我fragment有 2EditTexts和一个按钮。
我想onClick为我的按钮编写一个方法,但我希望在我的按钮class中使用它,以便我可以在我使用我的fragment(动态)的任何地方使用它。我在LoginPageFragment课堂上写了方法的主体,但程序无法识别它。也不知道这里有什么问题?
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginPageFragment extends Fragment {
private Button btnLogin ;
private EditText inputUsername , inputPassword ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_login, container , false);
inputUsername = (EditText) fragmentView.findViewById(R.id.input_username);
inputPassword = (EditText) fragmentView.findViewById(R.id.input_password);
btnLogin = (Button) fragmentView.findViewById(R.id.btn_login);
return fragmentView ;
}
public void onLoginClick(View v){
String usernameString = inputUsername.getText().toString() ;
String passwordString = inputPassword.getText().toString() ;
if(Utility.areEditTextsEmpty(inputUsername , inputPassword)){
Toast.makeText(LoginActivity.fa , "Some Fields are EMPTY",Toast.LENGTH_SHORT).show();
}
User tempUser = Utility.doesTheUserExist(usernameString , passwordString);
if(tempUser != null){
Utility.sendUserInfoToAnotherActivity(tempUser);
}
else{
Toast.makeText(LoginActivity.fa , "User Not Defined" , Toast.LENGTH_SHORT);
}
}
}
一只名叫tom的猫
相关分类