在Kotlin中,我想在Java中添加一个与此RequiresPermission
注释等效的方法注释,表明需要多个权限:
@RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
public Location getLocation() {
// ...
}
如何在Kotlin中编写此注释?
答案 0 :(得分:5)
您可以使用using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
//Some simulation code clcick button 2/3
//Button1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button1.Clicked");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Button2.Clicked");
}
}
}
传递项目数组作为注释参数:
arrayOf
您只需将Java代码粘贴到Kotlin文件Android Studio中即可实现此解决方案。
更新:since Kotlin 1.2,您也可以使用array literal syntax:
@RequiresPermission(allOf = arrayOf(ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION))
fun getLocation(): Location {
// ...
}