我正在开发一个包含自定义拨号程序片段的应用程序。该片段的功能应该与股票拨号器相同。当我按下一个按钮时,那个数字就会出现在 editText 窗口上,就这么简单。
但是,editText 上没有出现任何数字。我首先使用活动而不是片段尝试了这个,它奏效了。现在我被迫将活动更改为片段,并且我一直在调整代码以适应片段环境,但是这个功能不起作用。
这是PhoneView
片段:
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;
}
不得不说,例如拨号按钮可以工作,不起作用的是在edittext上显示按下的数字。这可能与片段的任何限制或特殊功能有关吗?
更新->在上面添加了更多代码,并遵循 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>
拨号盘.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>