我想用一个按钮和一个固定号码打电话

vvppvyoh  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(377)

我想用一个按钮打电话。电话号码应该在里面。但我的密码不起作用。谁能支持?
public class mainactivity扩展appcompatactivity{private button;

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. Button button = (Button) findViewById(R.id.button);
  6. }
  7. public void onClickStart(View view) {
  8. button = findViewById(R.id.button);
  9. ((Button)findViewById(R.id.button)).setOnClickListener((View.OnClickListener) v -> {
  10. Intent callIntent = new Intent(Intent.ACTION_CALL);
  11. String phNum = "tel:" + "89261234567";
  12. callIntent.setData(Uri.parse(phNum));
  13. startActivity( callIntent) ;
  14. });
  15. }

}

清单文件

  1. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  2. <application
  3. android:allowBackup="true"
  4. android:icon="@mipmap/ic_launcher"
  5. android:label="@string/app_name"
  6. android:roundIcon="@mipmap/ic_launcher_round"
  7. android:supportsRtl="true"
  8. android:theme="@style/Theme.GateApp">
  9. <activity android:name=".MainActivity">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. </application>
egmofgnx

egmofgnx1#

您的问题是,您从未将click侦听器分配给按钮,因为onclickstart从未被调用。你应该这样做

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. Button button = (Button) findViewById(R.id.button);
  6. button.setOnClickListener((View.OnClickListener) v -> {
  7. Intent callIntent = new Intent(Intent.ACTION_CALL);
  8. String phNum = "tel:" + "89261234567";
  9. callIntent.setData(Uri.parse(phNum));
  10. startActivity( callIntent) ;
  11. });
  12. }

这样您就可以将click侦听器分配给按钮。在文档中您可以找到如何使用它。
这里是调用意图文档的链接

展开查看全部
cqoc49vn

cqoc49vn2#

我想你可以把这个字符串<数据android:scheme=“电话”/>

c9x0cxw0

c9x0cxw03#

只需在onclicklistener中使用以下两行代码:

  1. ((Button)findViewById(R.id.button)).setOnClickListener((View.OnClickListener) v -> {
  2. String phNum = "tel:" + "89261234567";
  3. startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phNum, null)));
  4. });

相关问题