android片段setOnClickListener

时间:2013-02-20 09:58:31

标签: android android-layout android-fragments

我使用片段,当我点击一个editext时,会显示一个特殊的键盘,(许多按钮可点击),但我找不到他正确的方法来检测点击按钮

这是onCreateView代码:

        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.fragment_rsslist_overview,container, false);
                try {
                    setKeys();

                } catch (Exception e) {
                    Log.w(getClass().getName(), e.toString());
                }
        //

        return view;
    }

这是setKeys代码:(我在mBSpace按钮上尝试了很多东西)

        public void setKeys() {
        mWindowWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth(); // getting
        // window
        // height
        // getting ids from xml files
        mB[0] = (Button) getActivity().findViewById(R.id.xA);
        mB[1] = (Button) getActivity().findViewById(R.id.xB);
        mB[2] = (Button) getActivity().findViewById(R.id.xC);
        mB[3] = (Button) getActivity().findViewById(R.id.xD);
        mB[4] = (Button) getActivity().findViewById(R.id.xE);
        mB[5] = (Button) getActivity().findViewById(R.id.xF);
        mB[6] = (Button) getActivity().findViewById(R.id.xG);
        mB[7] = (Button) getActivity().findViewById(R.id.xH);
        mB[8] = (Button) getActivity().findViewById(R.id.xI);
        mB[9] = (Button) getActivity().findViewById(R.id.xJ);
        mB[10] = (Button) getActivity().findViewById(R.id.xK);
        mB[11] = (Button) getActivity().findViewById(R.id.xL);
        mB[12] = (Button) getActivity().findViewById(R.id.xM);
        mB[13] = (Button) getActivity().findViewById(R.id.xN);
        mB[14] = (Button) getActivity().findViewById(R.id.xO);
        mB[15] = (Button) getActivity().findViewById(R.id.xP);
        mB[16] = (Button) getActivity().findViewById(R.id.xQ);
        mB[17] = (Button) getActivity().findViewById(R.id.xR);
        mB[18] = (Button) getActivity().findViewById(R.id.xS);
        mB[19] = (Button) getActivity().findViewById(R.id.xT);
        mB[20] = (Button) getActivity().findViewById(R.id.xU);
        mB[21] = (Button) getActivity().findViewById(R.id.xV);
        mB[22] = (Button) getActivity().findViewById(R.id.xW);
        mB[23] = (Button) getActivity().findViewById(R.id.xX);
        mB[24] = (Button) getActivity().findViewById(R.id.xY);
        mB[25] = (Button) getActivity().findViewById(R.id.xZ);
        mB[26] = (Button) getActivity().findViewById(R.id.xS1);
        mB[27] = (Button) getActivity().findViewById(R.id.xS2);
        mB[28] = (Button) getActivity().findViewById(R.id.xS3);
        mB[29] = (Button) getActivity().findViewById(R.id.xS4);
        mB[30] = (Button) getActivity().findViewById(R.id.xS5);
        mB[31] = (Button) getActivity().findViewById(R.id.xS6);
        mBSpace = (Button) getActivity().findViewById(R.id.xSpace);
        mBdone = (Button) getActivity().findViewById(R.id.xDone);
        mBChange = (Button) getActivity().findViewById(R.id.xChange);
        mBack = (Button) getActivity().findViewById(R.id.xBack);
        mNum = (Button) getActivity().findViewById(R.id.xNum);
//      for (int i = 0; i < mB.length; i++)
        mBSpace.setOnClickListener( this);
/*      
        mBSpace.setOnClickListener( new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    edittext_onClick(v );
                }
              });
*/          
/*          
            mB[i].setOnClickListener( this);
        mBSpace.setOnClickListener( this);
        mBdone.setOnClickListener( this);
        mBack.setOnClickListener( this);
        mBChange.setOnClickListener( this);
        mNum.setOnClickListener( this);
//      mNum.setOnClickListener( this);
*/      


    }

有人知道他的语法正确吗? 我尝试将setkey()调用到:

    public void onActivityCreated (Bundle savedInstanceState){
    setKeys();
    super.onActivityCreated(savedInstanceState);
}

我没有错误,但也没有点击检测...... Thx提前......

2 个答案:

答案 0 :(得分:1)

您是否在EditText的xml元素中尝试了android:clickable =“true”?

答案 1 :(得分:0)

确保特殊键盘布局是当前contentView的一部分。如果不是,并且您将其膨胀到当前视图中,那么当您获得对按钮的引用时,您应该使用

buttonA = inflatedLayout.findViewById(R.id.button);

还创建一个公共的onClickListener并将所有按钮绑定到它

OnClickListener buttonClickListener = new OnClickListener() {

      @Override
            public void onClick(View v) {

                edititext.setText(v.getTag().toString()); 
                // or whatever logic you are using

            }

}