我試圖用谷歌搜索我的問題,但找不到,這就是我問的原因。
錯誤:未解決的參考:AlertDialog
問題:如何匯入 AlertDialog,因為如果我用谷歌搜索它,然后我會看到導致上述錯誤的不同方法,例如import android.app.AlertDialog或import androidx.appcompat.app.AlertDialog其他東西,我需要一些永遠不會損壞的通用東西。我想知道為什么#include <...>in C 永遠不會過期并且會持續很多年。
從https://geeksforgeeks.org/how-to-create-a-custom-yes-no-dialog-in-android-with-kotlin/匯入
import android.app.AlertDialog
從https://www.geeksforgeeks.org/how-to-create-an-alert-dialog-box-in-android/匯入
import androidx.appcompat.app.AlertDialog;
從https://www.digitalocean.com/community/tutorials/android-alert-dialog-using-kotlin匯入
import android.support.v7.app.AlertDialog;
“MainActivity.kt”中的代碼(Android Studio Dolphin 中的 Kotlin 和 C | 2021.3.1 補丁 1)
package com.emcengine.emceditor
import android.app.NativeActivity
import android.os.Bundle
import android.content.Context
import android.view.inputmethod.InputMethodManager
import android.view.KeyEvent
import java.util.concurrent.LinkedBlockingQueue
//_/--------------------------------------------------\_
import android.app.AlertDialog // error: unresolved reference: AlertDialog
// \--------------------------------------------------/
class MainActivity : NativeActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
fun showSoftInput() {
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(this.window.decorView, 0)
}
fun hideSoftInput() {
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(this.window.decorView.windowToken, 0)
}
// Queue for the Unicode characters to be polled from native code (via pollUnicodeChar())
private var unicodeCharacterQueue: LinkedBlockingQueue<Int> = LinkedBlockingQueue()
// We assume dispatchKeyEvent() of the NativeActivity is actually called for every
// KeyEvent and not consumed by any View before it reaches here
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.action == KeyEvent.ACTION_DOWN) {
unicodeCharacterQueue.offer(event.getUnicodeChar(event.metaState))
}
return super.dispatchKeyEvent(event)
}
fun pollUnicodeChar(): Int {
return unicodeCharacterQueue.poll() ?: 0
}
//_/--------------------------------------------------\_
fun messageBox() {
AlertDialog.Builder builder // error: unresolved reference: AlertDialog
//builder = new AlertDialog.Builder(this)
}
// \--------------------------------------------------/
}
uj5u.com熱心網友回復:
一般來說:您不必手動添加匯入。當您只參考另一個類等時,AndroidStudio 會為您完成所有這些作業。通過洗掉匯入并簡單地在AlertDialog某處鍵入來嘗試它。然后從下拉串列的建議串列中選擇它。
您可能正在尋找的是import androidx.appcompat.app.AlertDialog.
此外,在 Kotlin 中,型別位于變數名之后(在這種情況下是可選的):
val builder: AlertDialog = AlertDialog.Builder(context)
uj5u.com熱心網友回復:
import android.app.AlertDialog
舊AlertDialog類,不應該再使用了。出于追溯兼容性的目的仍然存在。
import android.support.v7.app.AlertDialog
AlertDialog在支持新 API 的同時適用于新舊 Android 版本。這需要對支持 v7 庫的新依賴。
import androidx.appcompat.app.AlertDialog
Android X(又名新 SDK)AlertDialog。這是您要使用的那個。
更多關于這個
uj5u.com熱心網友回復:
如果您正在進行 Android 開發,請務必了解這一點:Android 有很多版本,因為許多設備僅在幾年后就停止更新。這意味著新的 Android 功能,以及對舊功能的更新和修復,在很多設備上都不存在。
當您嘗試撰寫適用于所有這些設備的軟體時,這就是一個問題 - 有些東西將無法使用。有些東西會以不同的方式作業,或者以不同的方式顯示。有些東西會有錯誤,這些錯誤會在以后的版本中修復,您必須解決這些問題。
充其量,如果 API 版本介于此檢查和此檢查之間,您將不得不做很多事情,不同版本的代碼路徑不同。在最壞的情況下,有些東西根本不可用,您將不得不決定是不使用這些功能,將您的應用程式鎖定在可用的設備范圍內,還是有效地制作您的應用程式的多個版本。然后你也必須測驗它!
所以 Android 的一些非常好的人制作的是支持庫。這基本上是向后移植功能和修復,因此它們可以在舊設備上使用,而不是只能使用他們擁有的操作系??統版本中的東西。它還為您提供了這些功能的通用介面——您不需要Compat為舊 API 使用庫,為新 API 使用系統版本,您只需使用Compat版本即可。它會在內部確定要做什么,您無需擔心。
他們還測驗了所有這些功能,因此您可以假設它們只適用于每個 API - 您不需要進行特殊測驗來查看您自己的解決方法是否得到正確處理,有人已經為支持庫實作完成了這些作業!
因此,推薦的標準做法是始終在您的 Android 專案中使用支持庫,并且始終使用其實作而不是標準庫。這就是為什么你Activities繼承自AppCompatActivity而不是Activity,以及為什么Fragment自動匯入的類是那個androidx,而不是標準的Fragment(實際上在幾個版本前就被棄用了)
至于您的匯入,問題在于您正在閱讀相隔數年的教程和指南,并且情況會發生變化。m0skit0的回答解釋了每個版本是什么——但基本上該support庫已被棄用,您需要新的AndroidX/Jetpack版本。
AndroidX 有點不同,它不是一個大的支持庫(根據 API 版本分成少數仍然大的支持庫),它更細化。這意味著您必須安裝所需的特定庫。AlertDialog(和Material Design 版本)是androidx.appcompat:appcompat庫的一部分(頁面頂部,或包名稱中) - 因此您需要將其添加為依賴項,這就是您將在應用程式中使用的匯入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537161.html
標籤:安卓工作室科特林用户界面
上一篇:JavaScript:根據n級嵌套屬性值從陣列中過濾出物件的最佳解決方案
下一篇:如何在Kotlin中將按鈕的文本設定為“thisviewsstring”和“thisotherviewsstring”
