我有一个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); } }}
2 回答
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
xml 中的 onClick 标签需要由活动处理。在片段中,最简单的方法就是以编程方式添加点击监听器。另一种方法是让活动处理它,然后在片段上调用一个函数,但这会让人头疼,如果你改变片段,可能会出现问题。
添加回答
举报
0/150
提交
取消