【问题标题】:Android: button action inside fragmentAndroid:片段内的按钮操作
【发布时间】:2017-07-24 12:11:10
【问题描述】:

我正在尝试从片段中的按钮触发操作。代码没有指出错误,但是我的 Toast 从未被触发。

你知道下面的方法是否有意义吗?

这些是我的片段方法:

View.OnClickListener myAction = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "AccountView", Toast.LENGTH_SHORT).show();
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_account_view, container, false);
    accountButton = (Button) view.findViewById(R.id.button);
    accountButton.setOnClickListener(myAction);

    return inflater.inflate(R.layout.fragment_account_view, container, false);
}

【问题讨论】:

  • 在返回过程中,您再次膨胀视图...这会使先前的侦听器无效...
  • 因为你返回 inflater.inflate(R.layout.fragment_account_view, container, false);所以进一步初始化所以只返回返回视图。

标签: android button fragment


【解决方案1】:

试试这个

请将您的 onCreateView 替换为以下代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_account_view, container, false);
    accountButton = (Button) view.findViewById(R.id.button);
    accountButton.setOnClickListener(myAction);

    return view; //change here
}

这是因为您正在再次创建视图。那就是你返回inflater.inflate(R.layout.fragment_account_view, container, false);

【讨论】:

    【解决方案2】:

    你应该返回膨胀的视图:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
    
        View view = inflater.inflate(R.layout.fragment_account_view, container, false);
        accountButton = (Button) view.findViewById(R.id.button);
        accountButton.setOnClickListener(myAction);
    
        return view;
    }
    

    【讨论】:

    • 谢谢你的回答,它可能会解决它,但之前的答案是第一位的,因此我选择它作为答案。
    • 我很确定我是第一个回答的人,但没关系,我很高兴你解决了这个问题。
    【解决方案3】:

    返回片段中的视图对象:

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
    
        View view = inflater.inflate(R.layout.fragment_account_view, container, false);
        accountButton = (Button) view.findViewById(R.id.button);
        accountButton.setOnClickListener(myAction);
    
        return view;
    }
    

    用于展示吐司的用途:

        View.OnClickListener myAction = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "AccountView", Toast.LENGTH_SHORT).show();
        }
    };
    

    【讨论】:

    • 谢谢你的回答,它可能会解决它,但之前的答案是第一位的,因此我选择它作为答案。
    【解决方案4】:
     @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {   
      View view = inflater.inflate(R.layout.fragment_account_view,container,false);
      accountButton = (Button) view.findViewById(R.id.button);
      accountButton.setOnClickListener(myAction);
    
      return view;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多