Android 片段:EditText setOnFocusChangeListener

问题描述 投票:0回答:1

我正在挣扎的情况:

一个 MainActivity 和四个片段......简而言之,如何在片段中包含的给定 EditText 字段上设置丢失焦点事件?

在简单的活动中,这非常有效。

protected void onCreate(Bundle savingInstanceState) {

oET_PSN = findViewById(R.id.txtEqPSN);

oET_PSN.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
       if(!hasFocus){

             Log.d("oET_PSN", "Lost Focus");
            
          }
      }
 })

.... ....

但是如何让它在片段中工作?

各种youtube;各种 stackoverflow 帖子... ...我正在寻找一个可以轻松复制的简单示例

android-fragments android-edittext
1个回答
0
投票

我的问题的解决方案如下:(希望对某人有帮助)

public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savingInstanceState){ 查看 v = inflater.inflate(R.layout.fragment_build, 容器, false);

oET_PSN = (EditText)v.findViewById(R.id.txtEqPSN);
oET_DESC = (EditText)v.findViewById(R.id.txtEqPSNDescription);

oET_PSN.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(!hasFocus){
            Log.d("LUCK", "Lost Focus");
            //call function on getPSNDescription method created in main activity and set the text on the EditText in Fragment 
            oET_DESC.setText ( ((MainActivity) getActivity()).getPSNDescription("BuildFragment",oET_PSN.getText().toString()) );


        }
    }
});

return v;

}

© www.soinside.com 2019 - 2024. All rights reserved.