如何拨打电话编程方式的Android? [英] How to make phone calls programmatically in Android?
问题描述
我在useful_numbers_item_fragment.xml定义以下布局:
I have the following layout defined in useful_numbers_item_fragment.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/call_linear_layout">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/useful_nums_item_name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/useful_nums_item_value"/>
</LinearLayout>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/call"
android:id="@+id/call_btn"
android:onClick="callNumber"/>
</LinearLayout>
我动态填充在一个名为UNItemListFragment.java类两个文本的意见
在onCreate方法:
I dynamically populate the two text views in a class called UNItemListFragment.java in the onCreate method:
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
if (getArguments().containsKey(Constants.UNItem.GROUP_ID)) {
simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.useful_numbers_item_fragment, null,
new String[]{Constants.UNItem.NAME, Constants.UNItem.VALUE},
new int[]{R.id.useful_nums_item_name, R.id.useful_nums_item_value}, 0);
setListAdapter(simpleCursorAdapter);
getLoaderManager().initLoader(0, getArguments(), this);
}
}
对于每个编号,如果我按一下按钮我想打一个电话
调用callNumber方法,当用户点击这个按钮:
For each number if i click on the button i want to make a phone call by calling the callNumber method when the user clicks the button:
public void callNumber(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
这是确定当我单击列表中的第一个按钮,但是当我点击其他按钮
它继续呼叫第一行中所定义的数量...
It is ok when I click the first button in the list, but when I click on the other buttons it continues calling the number defined in the first row...
不知道如何解决这个问题?
Any idea how to resolve this?
推荐答案
的问题是,这一行:
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
是在活动执行,因此 findViewById
将始终与ID,它很可能在列表中的第一项返回的第一个项目。
is executed on the activity, so the findViewById
will always return the first item with that id, which is likely the first item in the list.
的最的方式来解决,这将是覆盖适配器并添加包含电话号码,到视图的标签。 A 快速的办法解决这一问题将是在视图层次尾随,像这样:
The best way to fix this would be to override the adapter and add a tag containing the phone number to the view. A quick way to fix this would be to tag along in the view hierarchy, like so:
public void callNumber(View view) {
if( view != null ) { // view is the button tapped
View parent = view.getParent(); // this should be the LinearLayout
if( parent instanceof LinearLayout ) {
TextView unItemVal = (TextView) ((LinearLayout)parent).findViewById(R.id.useful_nums_item_value);
if( unItemVal != null ) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
}
}
}
这会找到被点击的按钮父,然后找到包含内的数字文本视图的ViewGroup
。
This would find the parent for the button that was clicked, and then find the text-view containing the number within that ViewGroup
.
这篇关于如何拨打电话编程方式的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!