Correct way to call onClickListener on fragments

2019-08-05 03:52发布

问题:

I'm developing an app that contains a custom dialer fragment. The functionality of this fragment should be the same as the stock dialer. When I push a button, that number appears on the editText window, as simple as that.

But, it doesn't appear no number on the editText. I first tried this using Activities instead of fragments and it worked. Now I've been forced to change the activities to fragments, and I've been adapting the code to the fragments eviroment, but this functionality doesn't work.

This is the PhoneView fragment:

public class PhoneView extends Fragment implements OnTabChangeListener, OnClickListener {

    ImageButton dialBtn;
    ImageButton clearBtn;
    EditText numTxt;
    ImageButton button1;
    ImageButton button2;
    ImageButton button3;
    ImageButton button4;
    ImageButton button5;
    ImageButton button6;
    ImageButton button7;
    ImageButton button8;
    ImageButton button9;
    ImageButton button0;
    ImageButton buttonstar;
    ImageButton buttonpound;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRoot = inflater.inflate(R.layout.phone_view, null);

        dialBtn = (ImageButton) mRoot.findViewById(R.id.dialButton);
        clearBtn = (ImageButton) mRoot.findViewById(R.id.deleteButton);
        numTxt = (EditText) mRoot.findViewById(R.id.digits);
        button1 = (ImageButton) mRoot.findViewById(R.id.one);
        button2 = (ImageButton) mRoot.findViewById(R.id.two);
        button3 = (ImageButton) mRoot.findViewById(R.id.three);
        button4 = (ImageButton) mRoot.findViewById(R.id.four);
        button5 = (ImageButton) mRoot.findViewById(R.id.five);
        button6 = (ImageButton) mRoot.findViewById(R.id.six);
        button7 = (ImageButton) mRoot.findViewById(R.id.seven);
        button8 = (ImageButton) mRoot.findViewById(R.id.eight);
        button9 = (ImageButton) mRoot.findViewById(R.id.nine);
        button0 = (ImageButton) mRoot.findViewById(R.id.zero);
        buttonstar = (ImageButton) mRoot.findViewById(R.id.star);
        buttonpound = (ImageButton) mRoot.findViewById(R.id.pound);

        dialBtn.setOnClickListener(this);
        clearBtn.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button0.setOnClickListener(this);
        buttonstar.setOnClickListener(this);
        buttonpound.setOnClickListener(this);

        return mRoot;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialButton:
                startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + numTxt.getText())));
                break;
            case R.id.deleteButton:
                String contents = numTxt.getText().toString();
                if (contents.length() > 0) {
                    numTxt.setText(contents.substring(0, contents.length()-1));
                }
                break;
            case R.id.one:
                numTxt.setText(numTxt.getText()+"1");
                break;
            case R.id.two:
                numTxt.setText(numTxt.getText()+"2");
                break;
            case R.id.three:
                numTxt.setText(numTxt.getText()+"3");
                break;
            case R.id.four:
                numTxt.setText(numTxt.getText()+"4");
                break;
            case R.id.five:
                numTxt.setText(numTxt.getText()+"5");
                break;
            case R.id.six:
                numTxt.setText(numTxt.getText()+"6");
                break;
            case R.id.seven:
                numTxt.setText(numTxt.getText()+"7");
                break;
            case R.id.eight:
                numTxt.setText(numTxt.getText()+"8");
                break;
            case R.id.nine:
                numTxt.setText(numTxt.getText()+"9");
                break;
            case R.id.zero:
                numTxt.setText(numTxt.getText()+"0");
                break;
            case R.id.star:
                numTxt.setText(numTxt.getText()+"*");
                break;
            case R.id.pound:
                numTxt.setText(numTxt.getText()+"#");
                break;
        }

Have to say that for example the dial button works, what doesn't work is the showing of the pressed numbers on the edittext. Could be this related to any restriction or special functionality of the fragments?

UPDATED -> Added more code above, and following the xml

phone_view.xml

<!-- Layout for the dialer -->
<LinearLayout 
    android:id="@+id/Right_layout"
    android:layout_width="fill_parent"        
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_marginStart="@dimen/dialpad_horizontal_margin"
    android:background="#000000" > 

    <!-- Text field above the keypad where the digits are displayed -->
    <LinearLayout
        android:id="@+id/digits_container"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="@integer/dialpad_layout_weight_digits"
        android:layout_marginTop="@dimen/dialpad_vertical_margin"
        android:gravity="center"
        android:background="@drawable/dialpad_background" >

        <EditText
            android:id="@+id/digits"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:scrollHorizontally="true"
            android:textAppearance="@style/DialtactsDigitsTextAppearance"
            android:focusableInTouchMode="false"
            android:nextFocusRight="@+id/deleteButton"
            android:background="@android:color/transparent" 
            android:inputType="none" />

        <ImageButton
            android:id="@+id/deleteButton"
            android:layout_width="56dip"
            android:layout_height="50dip"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:contentDescription="@string/description_delete_button"
            android:src="@drawable/ic_dial_action_delete" 
            android:background="@drawable/dialbtn_push"/>
    </LinearLayout>

    <!-- The dialpad itself -->
    <include layout="@layout/dialpad" />

    <View
       android:layout_width="match_parent"
       android:layout_height="@dimen/dialpad_vertical_margin"
       android:background="#66000000"/>

    <FrameLayout
        android:id="@+id/dialButtonContainer"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="@integer/dialpad_layout_weight_additional_buttons"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/dialpad_background">

        <ImageButton
            android:id="@+id/dialButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/btn_call"
            android:contentDescription="@string/description_dial_button"
            android:src="@drawable/ic_dial_action_call" />

</FrameLayout>        
</LinearLayout>

dialpad.xml

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialpad"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="@integer/dialpad_layout_weight_dialpad"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/dialpad_vertical_margin"
android:paddingStart="5dip"
android:paddingEnd="5dip"
android:paddingBottom="10dip"
android:background="@drawable/dialpad_background"
android:layoutDirection="ltr">

<TableRow
     android:layout_height="0px"
     android:layout_weight="1" >
    <ImageButton 
        android:id="@+id/one" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_1_no_vm_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_one" />
    <ImageButton 
        android:id="@+id/two" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_2_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_two" />
    <ImageButton 
        android:id="@+id/three" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_3_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_three" />
</TableRow>

<TableRow
     android:layout_height="0px"
     android:layout_weight="1">
    <ImageButton 
        android:id="@+id/four" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_4_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_four" />
    <ImageButton 
        android:id="@+id/five" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_5_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_five" />
    <ImageButton 
        android:id="@+id/six" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_6_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_six" />
</TableRow>

<TableRow
     android:layout_height="0px"
     android:layout_weight="1">
    <ImageButton 
        android:id="@+id/seven" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_7_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_seven" />
    <ImageButton 
        android:id="@+id/eight" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_8_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_eight" />
    <ImageButton 
        android:id="@+id/nine" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_9_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_nine" />
</TableRow>

<TableRow
     android:layout_height="0px"
     android:layout_weight="1">
    <ImageButton 
        android:id="@+id/star" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_star_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_star" />
    <ImageButton 
        android:id="@+id/zero" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_0_no_plus_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_zero" />
    <ImageButton 
        android:id="@+id/pound" 
        style="@style/DialpadButtonStyle"
        android:src="@drawable/dial_num_pound_wht"
        android:background="@drawable/dialbtn_push"
        android:contentDescription="@string/description_image_button_pound" />
</TableRow>

回答1:

Declare numTxt as a class member. You have

EditText numTxt = (EditText) mRoot.findViewById(R.id.digits);
// local to onCreateView

Change to

EditText numTxt;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRoot = inflater.inflate(R.layout.phone_view, null);

        ImageButton dialBtn = (ImageButton) mRoot.findViewById(R.id.dialButton);
        ImageButton clearBtn = (ImageButton) mRoot.findViewById(R.id.deleteButton);
        numTxt = (EditText) mRoot.findViewById(R.id.digits);

Edit : To the updated question

As i said in the comments i see nothing wrong with the code posted.

Since you said you can't set text to editext, i just ran your code. For testing purpose i set the launcher icon as background to ImageButton. I modified the ui for testing purpose.

But the logic is the same you used