本文介绍了片段(刷卡)和按钮侦听问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

首先请原谅我的Andr​​oid愚蠢的知识,因为我在这新的。
我们有一些codeS在这里改变与滑动手势卡口与3个片段名为:最高评分,游戏,电影。
这是源$ C ​​$ CS

这里是MainActivity活动:

 公共类MainActivity扩展FragmentActivity工具
        ActionBar.TabListener {    私人ViewPager viewPager;
    私人TabsPagerAdapter mAdapter;
    私人动作条动作条;
    //标签标题
    私有String []选项卡= {最高评分,游戏,电影};    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //动初始化
        viewPager =(ViewPager)findViewById(R.id.pager);
        动作条= getActionBar();
        mAdapter =新TabsPagerAdapter(getSupportFragmentManager());        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(假);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        //添加标签
        对于(字符串TAB_NAME:标签){
            actionBar.addTab(actionBar.newTab()的setText(TAB_NAME)
                    .setTabListener(本));
        }        / **
         *上刷卡viewpager做出选择相应的标签
         * * /
        viewPager.setOnPageChangeListener(新ViewPager.OnPageChangeListener(){            @覆盖
            公共无效使用onPageSelected(INT位置){
                //上改变页面
                //使受人尊敬的选项卡中选择
                actionBar.setSelectedNavigationItem(位置);
            }            @覆盖
            公共无效onPageScrolled(INT为arg0,ARG1浮球,诠释ARG2){
            }            @覆盖
            公共无效onPageScrollStateChanged(INT为arg0){
            }
        });
    }    @覆盖
    公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){
    }    @覆盖
    公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
        //在选项卡中选择
        //秀推崇片段视图
        viewPager.setCurrentItem(tab.getPosition());
    }    @覆盖
    公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){
    }}

通过这个适配器

 公共类TabsPagerAdapter扩展FragmentPagerAdapter {    公共TabsPagerAdapter(FragmentManager FM){
        超(FM);
    }    @覆盖
    公共片段的getItem(INT指数){        开关(指数){
        情况下0:
            //顶级片段活动
            返回新TopRatedFragment();
        情况1:
            //游戏片段活动
            返回新GamesFragment();
        案例2:
            //电影片段活动
            返回新MoviesFragment();
        }        返回null;
    }    @覆盖
    公众诠释的getCount(){
        //获取项目计数 - 等于标签数
        返回3;
    }}

这是主要的xml:

 < android.support.v4.view.ViewPager的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>< /android.support.v4.view.ViewPager>

一个片段的(别人都是这样)

 公共类GamesFragment扩展片段{    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        查看rootView = inflater.inflate(R.layout.fragment_games,集装箱,FALSE);        返回rootView;
    }
}

通过这个XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:背景=#ff8400>    <的TextView的android:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:比重=中心
        机器人:文字=设计游戏画面
        机器人:TEXTSIZE =20dp
        机器人:layout_centerInParent =真/>
< / RelativeLayout的>

现在,我们要添加在这个网页上的按钮看到这个XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:背景=#ff8400>    <的TextView的android:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:比重=中心
        机器人:文字=设计游戏画面
        机器人:TEXTSIZE =20dp
        机器人:layout_centerInParent =真/>    <按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=新按钮
            机器人:ID =@ + ID /按钮
            机器人:layout_alignParentTop =真
            机器人:layout_alignParentLeft =真/>
< / RelativeLayout的>

要做到这一点的方法之一是在GamesFragment.java定义按钮,监听器
这是不可能的,因为它不能在该类中定义

 最后Button按钮=(按钮)findViewById(R.id.button);
        b1.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                // 做一点事
            }
        });

第二个方法是合并GameFragment的java codeS与主codeS。和编辑的主要活动是这样的:

 的setContentView(R.layout.activity_main&安培; R.layout.fragment_games);

这是我不能写codeS正确。

第三种方式是,我们应该写我们的主要活动片段使用一种布局。

现在ü可以帮助我解决此问题?


解决方案

 最后Button按钮=(按钮)rootView.findViewById(R.id.button);
    b1.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            //如何在这里开始新的活动形式?????
        }
    });

这相当奏效。

First of all excuse my stupid knowledge of android, because I'm new in it.We have some codes here that changes tabs with swipe gesture with 3 Fragments that named: "Top Rated", "Games", "Movies".This is the source codesHere is MainActivity activity:

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

With this adapter

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new TopRatedFragment();
        case 1:
            // Games fragment activity
            return new GamesFragment();
        case 2:
            // Movies fragment activity
            return new MoviesFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

This is main xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

One of Fragments (others are like this)

public class GamesFragment extends Fragment {

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

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

        return rootView;
    }
}

With this XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff8400" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Games Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

Now we want to add a button in this page see this XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff8400" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Games Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"/>


</RelativeLayout>

One way to do this is define button and listener in GamesFragment.javathat is impossible, because it can't be define in that class.

final Button button = (Button) findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // do some thing
            }
        });

Second way is merge GameFragment java codes with main codes. And edit main activity like this:

setContentView(R.layout.activity_main & R.layout.fragment_games);

That I can't write codes correctly.

Third way is we should write our fragments in main activity that use one layout.

Now can u help me to solve this?

解决方案
final Button button = (Button) rootView.findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // How to start a new activity form here?????
        }
    });

This worked quite well.

这篇关于片段(刷卡)和按钮侦听问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:56