如何在android片段中单击按钮运行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20573971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:04:11  来源:igfitidea点击:

How to run code on a button click in android fragment

androidbuttonandroid-fragmentsonclick

提问by Finley Smith

I need help trying to make a app work. I set up a multitab interface using fragments(so it there are 4 tabs with diffrent contents) On the fragment i am working on i want to have 3 buttons which run a intent like this:

我需要帮助来尝试使应用程序正常工作。我使用片段设置了一个多标签界面(所以它有 4 个带有不同内容的标签)在我正在处理的片段上,我想要有 3 个按钮,它们运行这样的意图:

    Log.i(TAG, "Website Clicked");
            Intent websiteBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/"));
            startActivity(websiteBrowserIntent);

So i have 3 buttons, heres the .xml:

所以我有 3 个按钮,这是 .xml:

XML does not seem to work for some reason heres a pastebin: http://pastebin.com/gGuh7qb2

由于某种原因,XML 似乎不起作用,这是一个 pastebin:http: //pastebin.com/gGuh7qb2

And here is TopRatedFragment.java:

这是 TopRatedFragment.java:

package com.bordengrammar.bordengrammarapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TopRatedFragment extends Fragment {

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

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container,         false);

        return rootView;
    }
}

回答by Joel Fernandes

public class TopRatedFragment extends Fragment implements OnClickListener {

    Button btn;

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

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container,         false);

        btn = (Button) rootView.findViewById(R.id.myButton);
        btn.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        // implements your things
    }
}

P.S. A little search can get you answers.

PS 一点搜索可以得到你的答案。

[EDIT]

[编辑]

public class TopRatedFragment extends Fragment implements OnClickListener {

            Button btn;

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

                View rootView = inflater.inflate(R.layout.fragment_top_rated, container,         false);

                btn = (Button) rootView.findViewById(R.id.myButton);
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {



                    }
                });
                return rootView;
            }

Please read some tutorials - http://www.vogella.com/articles/AndroidFragments/article.html

请阅读一些教程 - http://www.vogella.com/articles/AndroidFragments/article.html