对于 Android Studio,如何在 Kotlin 中使用 FAB 将用户发送到搜索页面?
How to send user to a search page using FAB in Kotlin, for Android Studio?
谁能帮我写这段代码?
p0.btnPlay.setOnClickListener {
val songName = song.songName
val songArtist = song.songArtist
Toast.makeText(mCtx, "You clicked this button!", Toast.LENGTH_SHORT).show()
val webIntent = Intent(Intent.ACTION_WEB_SEARCH, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
startActivity(webIntent)
}
I am getting an error at startActivity(webIntent)
: Type mismatch:
inferred type is Intent but Context was expected
如何将用户引导至这样的 YouTube 页面? toast 消息工作正常,所以按钮点击连接到 XML。
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "https://www.youtube.com/results?search_query=$songArtist+$songName");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
如果您的 mCtx
是 Activity context
,则使用
try {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
mCtx.startActivity(webIntent)
} catch(ex: Exception) {
ex.stackTrace
}
如果您在 fragment 中,请尝试使用
try {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
activity!!.startActivity(webIntent)
} catch(ex: Exception) {
ex.stackTrace
}
谁能帮我写这段代码?
p0.btnPlay.setOnClickListener {
val songName = song.songName
val songArtist = song.songArtist
Toast.makeText(mCtx, "You clicked this button!", Toast.LENGTH_SHORT).show()
val webIntent = Intent(Intent.ACTION_WEB_SEARCH, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
startActivity(webIntent)
}
I am getting an error at
startActivity(webIntent)
: Type mismatch: inferred type is Intent but Context was expected
如何将用户引导至这样的 YouTube 页面? toast 消息工作正常,所以按钮点击连接到 XML。
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "https://www.youtube.com/results?search_query=$songArtist+$songName");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
如果您的 mCtx
是 Activity context
,则使用
try {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
mCtx.startActivity(webIntent)
} catch(ex: Exception) {
ex.stackTrace
}
如果您在 fragment 中,请尝试使用
try {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=$songArtist+$songName"))
activity!!.startActivity(webIntent)
} catch(ex: Exception) {
ex.stackTrace
}