问题描述
我正在开发包含自定义拨号片段的应用程序。该片段的功能应该是相同的库存拨号器。当我按下一个按钮,在出现的窗口的EditText上号,就这么简单。
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.
但是,它不会出现在EDITTEXT任何数。我第一次尝试使用这个活动,而不是碎片和它的工作。现在,我已经被迫改变活动支离破碎,而我已经适应code将片段eviroment,但此功能不起作用。
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.
这是 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;
}
不得不说,例如拨号按钮的作品,什么行不通是$ P $的显示上的EditText pssed数字。可这关系到碎片的任何限制或特殊功能?
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?
更新 - >添加了更多code以上,而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>
推荐答案
声明 numTxt
作为类成员。您有
EditText numTxt = (EditText) mRoot.findViewById(R.id.digits);
// local to onCreateView
更改为
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
正如我在评论中说,我认为没有错的code公布。
As i said in the comments i see nothing wrong with the code posted.
既然你说你可以不设置文本editext,我只是跑你code。出于测试目的,我设定的启动器图标为背景,以的ImageButton
。我修改了用户界面,用于测试目的。
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.
但其逻辑是,你使用
这篇关于正确的方法来调用onClickListener的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!