.xml
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拨打电话" android:id="@+id/phone" />
.java
package com.example.chenshuai.test322; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; /** * Created by chenshuai on 2016/3/28. */ public class Longclick extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.longclicklayout); Button bt = (Button)findViewById(R.id.phone); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Uri uri = Uri.parse("tel:11232"); Intent intent = new Intent(Intent.ACTION_DIAL,uri); startActivity(intent); } }); //长按监听 bt.setOnLongClickListener(new View.OnLongClickListener() { @Override //返回值代表是否已经处理结束,后面是否需要再处理 public boolean onLongClick(View v) { Uri uri = Uri.parse("tel:110"); Intent intent = new Intent(Intent.ACTION_CALL,uri); startActivity(intent); //true事件处理结束,后面不需要再处理 return true; } }); } }
最后直接打电话别忘给权限
Andriodminifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
结果为:点一下按钮为调打电话页面,长按为直接拨打电话