安卓:可以访问查看OnClickListener元素 [英] Android: get access to view elements in OnClickListener

查看:141
本文介绍了安卓:可以访问查看OnClickListener元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段容器以及与该片段的内容交互片段之外的一些按钮。当我按一下按钮,我需要的文本元素内的信息。这是我现在所拥有的,但它不工作。
在code来自片段寻呼机演示。

 按钮BM =(按钮)findViewById(R.id.bm);
bm.setOnClickListener(新OnClickListener(){
  公共无效的onClick(视图v){
    的EditText editTextIn1 =(EditText上)v.findViewById(R.id.editTextIn1);
  }
});

编辑
我用这个例子:

<$p$p><$c$c>http://developer.android.com/reference/android/support/v13/app/FragmentStatePagerAdapter.html公共类FragmentStatePagerSupport扩展FragmentActivity {
    静态最终诠释NUM_ITEMS = 10;    MyAdapter mAdapter;    ViewPager mPager;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_pager);        mAdapter =新MyAdapter(getSupportFragmentManager());        mPager =(ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);        //留意按钮点击。
        Button按钮=(按钮)findViewById(R.id.goto_first);
        button.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                mPager.setCurrentItem(0);
            }
        });
        按钮=(按钮)findViewById(R.id.goto_last);
        button.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                mPager.setCurrentItem(NUM_ITEMS-1);
            }
        });
    }    公共静态类MyAdapter扩展FragmentStatePagerAdapter {
        公共MyAdapter(FragmentManager FM){
            超(FM);
        }        @覆盖
        公众诠释的getCount(){
            返回NUM_ITEMS;
        }        @覆盖
        公共片段的getItem(INT位置){
            返回ArrayListFragment.newInstance(位置);
        }
    }    公共静态类ArrayListFragment扩展ListFragment {
        INT MNUM;        / **
         *创建CountingFragment的新实例,提供了数
         *作为参数。
         * /
        静态的newInstance ArrayListFragment(INT NUM){
            ArrayListFragment F =新ArrayListFragment();            //供应NUM输入作为参数。
            捆绑ARGS =新包();
            args.putInt(民,NUM);
            f.setArguments(参数);            返回F;
        }        / **
         *在创建,检索它的参数此实例的数量。
         * /
        @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            MNUM = getArguments()!= NULL? getArguments()调用getInt(民):1。
        }        / **
         *片段的UI仅仅是展示其简单的文本视图
         *实例号。
         * /
        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            视图V = inflater.inflate(R.layout.fragment_pager_list,集装箱,FALSE);
            观看电视= v.findViewById(R.id.text);
            ((的TextView)TV).setText(碎片#+ MNUM);
            返回伏;
        }        @覆盖
        公共无效onActivityCreated(捆绑savedInstanceState){
            super.onActivityCreated(savedInstanceState);
            setListAdapter(新ArrayAdapter&LT;串GT;(getActivity()
                    android.R.layout.simple_list_item_1,Cheeses.sCheeseStrings));
        }        @覆盖
        公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
            Log.i(FragmentList,项目点:+ ID);
        }
    }
}


解决方案

在您的onCLickListener在V是指按钮本身,而按钮调用findViewByID表示按钮正在通过其子(它不具有有的话)来找到一个EditText元素。如果code是一个活动运行,您可能能够做到:

 按钮BM =(按钮)findViewById(R.id.bm);
bm.setOnClickListener(新OnClickListener(){
  公共无效的onClick(视图v){
    的EditText editTextIn1 =(EditText上)findViewById(R.id.editTextIn1);
  }
});

另外,如果你有碎片的容器的引用(如的FrameLayout或某事),你可以做

 按钮BM =(按钮)findViewById(R.id.bm);
bm.setOnClickListener(新OnClickListener(){
  公共无效的onClick(视图v){
    的EditText editTextIn1 =(EditText上)myFragmentContainer.findViewById(R.id.editTextIn1);
  }
});

I have a Fragment container and some buttons outside the fragment which interact with the content of the fragment. When I click on the button, I need to get the information inside the text elements. This is what I have right now, but it doesn't work. The code comes from the fragment pager demos.

Button bm = (Button) findViewById(R.id.bm);
bm.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    EditText editTextIn1 = (EditText) v.findViewById(R.id.editTextIn1);
  }
});

edit I use this sample:

http://developer.android.com/reference/android/support/v13/app/FragmentStatePagerAdapter.html

public class FragmentStatePagerSupport extends FragmentActivity {
    static final int NUM_ITEMS = 10;

    MyAdapter mAdapter;

    ViewPager mPager;

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

        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.goto_first);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(0);
            }
        });
        button = (Button)findViewById(R.id.goto_last);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(NUM_ITEMS-1);
            }
        });
    }

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            return ArrayListFragment.newInstance(position);
        }
    }

    public static class ArrayListFragment extends ListFragment {
        int mNum;

        /**
         * Create a new instance of CountingFragment, providing "num"
         * as an argument.
         */
        static ArrayListFragment newInstance(int num) {
            ArrayListFragment f = new ArrayListFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        /**
         * When creating, retrieve this instance's number from its arguments.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        }

        /**
         * The Fragment's UI is just a simple text view showing its
         * instance number.
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Fragment #" + mNum);
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Log.i("FragmentList", "Item clicked: " + id);
        }
    }
}

解决方案

The v in your onCLickListener is referring to the Button itself, and calling findViewByID on the button means that the button is looking through its children (it doesn't have any) to find an EditText element. if this code is running in an activity, you may be able to do:

Button bm = (Button) findViewById(R.id.bm);
bm.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    EditText editTextIn1 = (EditText) findViewById(R.id.editTextIn1);
  }
});

alternately, if you have a reference to the fragment's container (like a framelayout or something), you can do

Button bm = (Button) findViewById(R.id.bm);
bm.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    EditText editTextIn1 = (EditText) myFragmentContainer.findViewById(R.id.editTextIn1);
  }
});

这篇关于安卓:可以访问查看OnClickListener元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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