[英]Rude behavior of AlertDialog in android
嘿 Guyz 請幫我擺脫 AlertDialog 的粗魯行為。
單擊它時,我的片段中有一個按鈕,我希望它顯示一個填充有 Edittext 的 AlertDialog。 即使我為它編碼。
new AlertDialog.Builder(getActivity())
.setView(R.layout.send_message)
.setTitle(R.string.sendTitle + bundle.getString(USER_EXTRAS.FULLNAME_EXTRA))
.setPositiveButton(R.string.send, null)
.show();
但它在第二行提示“調用需要 API 級別 21(當前最小值為 11)android.app.alertDialog.Builder#setView”
我想要 11 作為最低 API(我推薦的)。
編譯后我得到了錯誤。
java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setView
如果 API 11 不支持 setView(),我應該改用哪個?
有什么替代方法嗎? 以及為什么我會收到此異常。?
任何幫助將不勝感激。
android.app.AlertDialog 需要 API 級別 21,所以使用 android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog;
將AlertDialog
自定義布局設置為:
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.send_message, null);
new AlertDialog.Builder(getActivity())
.setView(dialogView)
....
問題發生,因為有兩個版本的setView方法提供AlertDialog.Builder :
API LEVEL 1提供了第一個setView(視圖視圖)方法,該方法將視圖對象作為參數。
API LEVEL 21提供了第二個setView (int layoutResId)方法,該方法以布局 ID 作為參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.