亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在哪里添加片段的方法?

在哪里添加片段的方法?

慕村225694 2021-09-12 10:31:58
我有一個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 標簽需要由活動處理。在片段中,最簡單的方法就是以編程方式添加點擊監聽器。另一種方法是讓活動處理它,然后在片段上調用一個函數,但這會讓人頭疼,如果你改變片段,可能會出現問題。


查看完整回答
反對 回復 2021-09-12
  • 2 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號