Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说测试AI写代码的能力,希望能够帮助你!!!。
我用Google Bard 和百度的文心一言分别生一段android和ios的拨打电话的代码,以下分别是Google Bard和 百度的文心一言生成代码的截图。
Google Bard 生成的Android拨打电话的代码
Google Bard生成的IOS拨打电话的代码
言心一言生成的Android拨打电话的代码
文心一言生成的IOS拨打电话的代码
Sure, here is the Kotlin code for dialing a phone in Android:
fun dialPhone(number: String) {
// Check if the user has granted permission to make phone calls.
if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// Request permission from the user.
requestPermissions(arrayOf(Manifest.permission.CALL_PHONE), 100)
} else {
// Call the number.
val intent = Intent(Intent.ACTION_CALL)
intent.data = Uri.parse("tel:$number")
startActivity(intent)
}
}
This code first checks if the user has granted permission to make phone calls. If the user has not granted permission, the code requests permission from the user. If the user has granted permission, the code calls the number.
Here is an example of how to use this code:
val number = "123-456-7890"
dialPhone(number)
Sure, here is the Swift code for dialing a phone in iOS:
func dialPhone(number: String) {
// Check if the user has granted permission to make phone calls.
if !UIApplication.shared.canOpenURL(URL(string: "tel://\(number)")!) {
// The user has not granted permission to make phone calls.
return
}
// Call the number.
let url = URL(string: "tel://\(number)")!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
This code first checks if the user has granted permission to make phone calls. If the user has not granted permission, the code does nothing. If the user has granted permission, the code calls the number.
Here is an example of how to use this code:
dialPhone("123-456-7890")
以下是百度文心一言生成的代码
用kotlin写一段android拨打电话的代码
以下是使用 Kotlin 编写的一个简单的 Android 拨打电话的示例代码:
fun doubleClick() { Intent i = Intent(Intent.ACTION_CALL).apply { putExtra("cid", "12345678") // 电话号码 putExtra("type", "MOBILE") // 电话类型(这里设置为 "MOBILE" 表示拨打手机) putExtra("number", "12345678") // 电话号码 putExtra("name", "") // 电话号码 putExtra("return_data", false) // 返回数据 } if (activity != null) { activity.startActivityForResult(i, REQUEST_CODE_PHONE_CALL) } } fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { if (requestCode == REQUEST_CODE_PHONE_CALL && resultCode == RESULT_OK) { val phoneNumber = data.getStringExtra("number") if (phoneNumber != null && phoneNumber.length() > 0) { // 拨打电话成功,可以在这里处理拨打电话的逻辑 Toast.makeText(this, "拨打电话成功", Toast.LENGTH_SHORT).show() } else { // 拨打电话失败,可以在这里处理拨打电话的逻辑 Toast.makeText(this, "拨打电话失败,请检查电话号码是否正确", Toast.LENGTH_SHORT).show() } } } @Override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn_double_click.setOnClickListener(view -> {在双击事件中,我们创建了一个 `Intent` 对象,并设置了一些参数,包括电话号码、电话类型、电话号码等。我们使用 `if` 语句来判断用户是否真正在操作手机,如果用户不在操作手机,则不会启动拨打电话的操作。 接下来,我们在 `onCreate()` 方法中将按钮 `btn_double_click` 设置为焦点监听器。当用户双击按钮时,会调用 `doubleClick()` 方法。 在 `doubleClick()` 方法中,我们首先判断 `tv_call_num` 是否为焦点,如果不是,则调用 `tv_call_num.requestFocus()` 方法将其设置为焦点,然后使用 `tv_call_num.postDelayed()` 方法等待 1 秒钟后清除焦点,以避免用户在操作手机时误拨打电话。 最后,我们在 `onActivityResult()` 方法中处理拨打电话的结果。当用户成功拨打电话时,会弹出一个提示框;当用户拨打电话失败时
用swift写一段ios拨打电话的代码
以下是一个简单的 iOS 拨打电话的 Swift 代码示例:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 创建电话视图 let phoneNumber = "12345678" // 替换为你的电话号码 let phoneNumberLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 20)) phoneNumberLabel.text = phoneNumber phoneNumberLabel.textAlignment = .center phoneNumberLabel.backgroundColor = UIColor.white phoneNumberLabel.numberOfLines = 0 phoneNumberLabel.lineBreakMode = .byWordWrapping phoneNumberLabel.minimumScaleFactor = 0.2 phoneNumberLabel.textSize = CGSize(width: 20, height: 20) // 将电话号码添加到标签视图中 view.addSubview(phoneNumberLabel) // 创建电话拨号器 let dialer = Dialer() dialer.addPhoneNumber(phoneNumber) { (success, error) in if success { print("拨号成功") } else { print("拨号失败") } } } // 拨打电话 func callPhoneNumber() { let dialer = Dialer() dialer.addPhoneNumber { (success, error) in if success { 你可以根据自己的需要修改电话号码、标签视图、拨号器等。
从以上一个简单的例子可以看到,在写代码方面,Google Bard明显更胜一筹,代码简洁正确,拿来就能运行,而文心一言则是处于入门边缘,代码完全不能运行,这方面的能力还有待提升啊。
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章