通过在xml布局文件中设置android:onClick=""来实现组件单击事件

在布局中出现android:onClick=""语句:

 <Button
        android:id="@+id/call_button"
        android:onClick="callphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/phonenumber_edit"
        android:text="callme" />

在你的Activity中只要实现callphone的方法即可:

private void callPhone() {
            //代码优化之phoneNumber判断是否为空
            String phoneNumber = phonenumber_edit.getText().toString().trim();
            //判断内容是否为空
            if(TextUtils.isEmpty(phoneNumber)){
                Toast.makeText(getApplicationContext(), "内容不能为空", Toast.LENGTH_LONG).show();
                return;
            }

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+phoneNumber));
            startActivity(intent);
        }

 

posted @ 2013-12-08 17:03  西北野狼  阅读(1348)  评论(0编辑  收藏  举报