简体   繁体   English

单击片段中的按钮时如何显示吐司消息?

[英]How to display a toast message on clicking a button in a fragment?

First of all, forgive me if this is a silly question because I am relatively new in this field. 首先,如果这是一个愚蠢的问题,请原谅我,因为我在这个领域还比较新。 So basically, I have an activity which contains a fragment. 因此,基本上,我有一个包含片段的activity This fragment has three buttons. 该片段具有三个按钮。 And when any of this buttons are clicked, I wanted to display specific Toast messages . 当单击任何一个此按钮时,我想显示特定的Toast messages I used the onClick() method which works fine in an activity. 我使用了onClick()方法,该方法在活动中效果很好。 But when I use the same in a fragment, the app crashes. 但是,当我在片段中使用相同的应用程序时,该应用程序将崩溃。 Please help me in this regard.Name of the Activity is "User.java" and its XML file is "activity_user.xml". 在这方面请帮帮我。活动的名称是“ User.java”,其XML文件是“ activity_user.xml”。 Name of Fragment "user_home.java" and its xml file is "user_home_layout.xml". 片段“ user_home.java”的名称及其xml文件为“ user_home_layout.xml”。
I have attached two images, first one contains the activity and its XML file while the second one contains the fragment and its sml file. 我附加了两个图像,第一个图像包含活动及其XML文件,第二个图像包含片段及其sml文件。

1. Activity 2. Fragment 1. 活动 2. 片段

You dint find button in your fragment, 您在片段中找到了查找按钮,

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.user_home_layout, container, false);

    btn1=(Button)view.findViewById(R.id.GOT);  
    btn2=(Button)view.findViewById(R.id.SH);  
    btn3=(Button)view.findViewById(R.id.TD);

return view;
}

and then implement click listener and and in Toast just use getActivity() 然后实现点击监听器,在Toast中只需使用getActivity()

Toast.makeText(getActivity(), "Game Of Thrones", Toast.LENGTH_SHORT).show();

Implement method 实施方法

public void ButtonClick(View v) {
switch(v.getId()) {

 case R.id.GOT:


  Toast.makeText(getActivity(), "Game of Thornes", Toast.LENGTH_LONG).show();

   break;


 case R.id.SH:


  Toast.makeText(getActivity(), "Sherlock", Toast.LENGTH_LONG).show();

   break;


 case R.id.TD:


  Toast.makeText(getActivity(), "True Detective", Toast.LENGTH_LONG).show();

   break;
   }
 }    

You need to set a custom OnClickListener to your button : 您需要为按钮设置自定义OnClickListener:

yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "your toast text", Toast.LENGTH_LONG).show();
            }
        });

In your activity: 在您的活动中:

Toast.makeText(YourActivity.this, "your message", Toast.LENGTH_SHORT).show();

In your Fragment: 在您的片段中:

Toast.makeText(this.getActivity(), "your message", Toast.LENGTH_SHORT).show();

@TonyMathew, welcome to the Android world. @TonyMathew,欢迎来到Android世界。 Your implementation of ButtonClick is wrong. 您对ButtonClick的实现是错误的。 This is an extensive tutorial of how you should use Fragments. 是有关如何使用片段的详尽教程。 There is an example of handling click events as well. 还有一个处理点击事件的示例。 Long story short, you need to find your buttons in your fragment xml layout by id. 长话短说,您需要按ID在片段xml布局中找到按钮。 DON'T USE BUTTON'S TEXT! 不要使用按钮的文字!

Please, search the web for info, before posting a question here. 请先在网上搜索信息,然后再在此处发布问题。 The chances are that you will find what you are looking for! 很有可能您会找到想要的东西!

PS Usually in Java you should name your class in the following way: ClassName.java and not class_name.java PS通常,在Java中,您应该通过以下方式命名您的类: ClassName.java而不是class_name.java

只需将getActivity()放在makeText()的上下文中,如下所示:

Toast.makeText(getActivity(), "Your message", Toast.LENGTH_SHORT).show();

The problem is that your method is not found in activity, and so, an exception is raised (the methods from fragments doesn't count here). 问题在于您的方法未在活动中找到,因此引发了异常(片段中的方法不在此处计算)。 To fix that you have 2 options: 要解决此问题,您有2个选择:

  1. Move the ButtonClick method in activity and do some small changes (you'll have to obtain in a different way the Context ) and keep the xml file the same; 在活动中移动ButtonClick方法并进行一些小的更改(您必须以不同的方式获取Context )并保持xml文件相同;
  2. Set onClickListener for button and handle the onClick event inside the listener. 为按钮设置onClickListener并处理侦听器内的onClick事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM