[英]How to make phone calls programmatically in Android?
我在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>
我在onCreate方法中动态填充一个名为UNItemListFragment.java的类中的两个文本视图:
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方法来打电话:
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);
}
单击列表中的第一个按钮即可,但是当我单击其他按钮时,它会继续调用第一行中定义的数字...
知道如何解决这个问题吗?
问题是这一行:
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
在活动上执行,因此findViewById
将始终返回具有该id的第一个项目,这可能是列表中的第一个项目。
解决此问题的最佳方法是覆盖适配器并将包含电话号码的标签添加到视图中。 解决此问题的一种快速方法是在视图层次结构中进行标记,如下所示:
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
中的数字的文本视图。
使用findViewById()
将返回具有指定id的活动或片段中的第一个视图。 如果这是ListView,它将对应于第一行。
有很多方法可以解决这个问题。 最快的(但肯定不是最漂亮的,因为它取决于布局)将使用相对于包含列表项的LinearLayout的findViewById()
。 假设view
是ImageButton,它将是这样的:
((View)view.getParent()).findViewById(R.id.useful_nums_item_value)
更优雅的解决方案是在适配器的getView()
设置一个标签,其中包含您需要的数据(在这种情况下,是要调用的电话号码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.