按钮的onClickListener在片段中无法工作。

huangapple go评论63阅读模式
英文:

onClickListener for a button not working in a fragment

问题

这是我的代码,我不知道为什么它不监听按钮事件。
下面的代码来自片段文件:

//  private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    lf=inflater;
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);
    Button button = (Button) view.findViewById(R.id.home_button);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent=new Intent(lf.getContext(),sunhan_activity.class);
            startActivity(intent);
        }
    });
    return inflater.inflate(R.layout.fragment_home, container, false);
}
英文:

this is my code and I don't know why it is not listening for the button events
the below code is from fragment file

//  private Button button;
    LayoutInflater lf;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        lf=inflater;
        View view = inflater.inflate(R.layout.fragment_home,
                container, false);
        Button button = (Button) view.findViewById(R.id.home_button);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent=new Intent(lf.getContext(),sunhan_activity.class);
                startActivity(intent);
            }
        });
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

答案1

得分: 2

看看你的代码,你就会明白为什么代码不起作用。实际上,你膨胀(inflate)了布局两次,所以最后一个将被执行,而你第一次膨胀的View view = inflater.inflate(R.layout.fragment_home, container, false);将不会被执行,因为你没有使用它。

只需像这样更改代码:

//  private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    lf = inflater;
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);
    Button button = (Button) view.findViewById(R.id.home_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(lf.getContext(), sunhan_activity.class);
            startActivity(intent);
        }
    });
    return view;
}
英文:

Look at your code and you will get idea why the code is not working. Actually you are inflating layout 2 times, so last one will be executed and your first inflate View view = inflater.inflate(R.layout.fragment_home,container, false); will not be executed as you have not used it.

Simply change code like this:

//  private Button button;
LayoutInflater lf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    lf=inflater;
    View view = inflater.inflate(R.layout.fragment_home,
            container, false);
    Button button = (Button) view.findViewById(R.id.home_button);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent=new Intent(lf.getContext(),sunhan_activity.class);
            startActivity(intent);
        }
    });
    return view;
}

答案2

得分: 1

  1. 你不需要为LayoutInflater声明额外的变量,因为你的onCreateView()已经有一个可用的。

  2. 在移除后,不要再次从inflater调用context,你的fragment有自己的context。所以在intent中用getContext()替换lf.getContext()。由于你从fragment中调用了一个activity,所以使用getActivity()而不是getContext()

  3. 最后不要再次返回新的inflater,你之前已经分配了一个给View。所以返回你从view中声明的变量。

将这个:

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

替换为:

return view;

最终代码应该如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    Button button = (Button) view.findViewById(R.id.home_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), sunhan_activity.class);
            startActivity(intent);
        }
    });

    return view;
}
英文:
  1. You don't have to declare extra variable for LayoutInflater because your onCreateView() already has one to use.

  2. Don't call context from inflater again after removing, your fragment has it's own context. So replace lf.getContext() with getContext() inside intent. Since you are calling an activity from fragment use getActivity() instead of getContext().

  3. Don't return new inflater again at the end, you have previously assigned one before to View. So return the varibale you have declared from view.

replace this

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

with

return view;

Finally this is how it should look like:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        Button button = (Button) view.findViewById(R.id.home_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), sunhan_activity.class);
                startActivity(intent);
            }
        });

        return view;
    }

huangapple
  • 本文由 发表于 2020年9月19日 19:35:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63968305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定