如何实现点击侦听器在android系统页面查看器按钮? [英] How to implement click listener for a button in android Page Viewer?

查看:113
本文介绍了如何实现点击侦听器在android系统页面查看器按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Android的一个简单的页面控制....我想从一个页面移动到另一个页面横向滚动,像Android设备的主屏幕。

我在XML中有多个布局像的main.xml layout_first.xml layout_second.xml layout_third.xml

现在我有一个简单的按钮,我的 layout_first.xml ,我要实现对按钮点击听众像

  button.setOnClickListener(新OnClickListener()
        {
            公共无效的onClick(视图v)
            {            }
        });

没有,我不知道在哪里把上面的code

下面是我的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=主布局
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?< android.support.v4.view.ViewPager
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / myfivepanelpager/>
< / LinearLayout中>

下面是我的layout_first.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / myTextView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=第一个布局
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    <按钮
        机器人:ID =@ + ID /按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=按钮/>< / LinearLayout中>

下面是我的layout_second.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / myTextView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=第二个布局
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?< / LinearLayout中>

下面是我的layout_third.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / myTextView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=第三布局
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?< / LinearLayout中>

下面是我的Java code

 公共类MainActivity扩展活动
{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        MyPagerAdapter适配器=新MyPagerAdapter();        ViewPager myPager =(ViewPager)findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(适配器);
        myPager.setCurrentItem(0);    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单)
    {
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    私有类MyPagerAdapter扩展PagerAdapter
    {
        @覆盖
        公众诠释getCount将()
        {
            // TODO自动生成方法存根
             返回3;
        }        公共对象instantiateItem(查看收集,INT位置)
        {
            LayoutInflater吹气=(LayoutInflater)collection.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。            INT渣油= 0;            开关(位置)
            {
            情况下0:
                渣油= R.layout.layout_first;
                打破;
            情况1:
                渣油= R.layout.layout_second;
                打破;
            案例2:
                渣油= R.layout.layout_third;
                打破;            }            查看查看= inflater.inflate(渣油,NULL);            ((ViewPager)集合).addView(查看,0);            返回视图。
        }         @覆盖
         公共无效destroyItem(查看为arg0,ARG1 INT,对象ARG2){
             ((ViewPager)为arg0).removeView((查看)ARG2);         }
         @覆盖
         公共布尔isViewFromObject(查看为arg0,ARG1对象){
             返回将arg0 ==((查看)ARG1);         }         @覆盖
         公共Parcelable saveState和(){
             返回null;
         }    }}


解决方案

查看= inflater.inflate(渣油,NULL); 添加以下内容:

 如果(位置== 0){
    view.findViewById(R.id.button).setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){        }
    });
}

I want to create a simple Page Control in android.... I want to move from one page to another page scrolling horizontally, like the home screen in android device.

I have multiple layout in xml like main.xml, layout_first.xml, layout_second.xml and layout_third.xml

Now I have a simple button in my layout_first.xml, I want to implement a click listener for the button like

button.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {

            }
        });

No I don't know where to put the above code

Here is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myfivepanelpager"/>
</LinearLayout>

Here is my layout_first.xml

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

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

</LinearLayout>

Here is my layout_second.xml

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

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

Here is my layout_third.xml

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

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Third Layout"
        android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

Here is my java code

public class MainActivity extends Activity 
{

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

        MyPagerAdapter adapter = new MyPagerAdapter();

        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    private class MyPagerAdapter extends PagerAdapter 
    {
        @Override
        public int getCount() 
        {
            // TODO Auto-generated method stub
             return 3;
        }

        public Object instantiateItem(View collection, int position) 
        {
            LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;

            switch (position) 
            {
            case 0:
                resId = R.layout.layout_first;
                break;
            case 1:
                resId = R.layout.layout_second;
                break;
            case 2:
                resId = R.layout.layout_third;
                break;

            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

         @Override
         public void destroyItem(View arg0, int arg1, Object arg2) {
             ((ViewPager) arg0).removeView((View) arg2);

         }


         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
             return arg0 == ((View) arg1);

         }

         @Override
         public Parcelable saveState() {
             return null;
         }

    }

}

解决方案

After View view = inflater.inflate(resId, null); add the following:

if(position == 0){
    view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
        public void onClick(View v){

        }
    });
}

这篇关于如何实现点击侦听器在android系统页面查看器按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆