2013-04-22 200 views
1

我的問題是,我在edittext字段中有一個帶有黑色光標的alertdialog。我想改變這種顏色爲白色。您可以通過以下方式輕鬆地將edittext的顏色設置爲白色:更改alertdialog中edittext的光標顏色

edittext.setTextColor(Color.WHITE); 

問題是,光標顏色仍然是黑色。所以我做了一些研究,發現有關更改在XML文件中的光標顏色一些文章通過添加以下代碼:

android:textCursorDrawable="@drawable/white_cursor" 

的white_cursor.xml看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <size android:width="1dp"/> 
    <solid android:color="#FFFFFF" /> 
    </shape> 

於是我產生的EditText在XML文件並做findById() - 功能:

edittext = (EditText) findViewById(R.id.eText); 

但是當節目() - 方法被調用,應用程序崩潰。有沒有人知道我可以如何改變我的代碼中的光標顏色,或者如何實現它,而沒有錯誤? THX

編輯1個alertdialog代碼:

input = (EditText) findViewById(R.id.eText); 
    InputFilter[] filters = new InputFilter[1]; 
    filters[0] = new InputFilter.LengthFilter(20); 
    input.setFilters(filters); 
    input.setTextColor(Color.WHITE); 

    alertbuilder = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_DARK); 
    alertbuilder.setTitle("Enter the levelname!"); 
    alertbuilder.setMessage("To change the levelname after it has been created, tap and hold the icon of the level."); 

    alertbuilder.setView(input); 

    alertbuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(final DialogInterface dialog, final int whichButton) { 
    Editable value = (Editable) input.getText(); 
     // Do stuff 
     dialog.dismiss(); 

     } 

    }); 

    alertbuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      dialog.dismiss(); 
     } 
    }); 
    alertadd=alertbuilder.create(); 
    alertadd.show(); //crash here 

EDIT2 logcat的:

04-22 20:00:34.217: D/OpenGLRenderer(2475): Flushing caches (mode 0) 
04-22 20:00:37.216: D/Input(2475): VelocityTracker: int datax = 15 
04-22 20:00:37.216: D/Input(2475): VelocityTracker: int m_velocity_magnify_x = 1.500000 
04-22 20:00:37.216: D/Input(2475): VelocityTracker: int datay = 20 
04-22 20:00:37.216: D/Input(2475): VelocityTracker: int m_velocity_magnify_y = 2.000000 

我沒有得到我的logcat的錯誤消息。其實,我得到一個類文件編輯器 - 源未找到 - 錯誤

+1

安置自己的警告對話框代碼... – Pragnani 2013-04-22 17:54:02

+0

郵政logcat的錯誤。 – TronicZomB 2013-04-22 17:54:46

+0

是你的edittext是你的佈局的一部分..?即它是由setContentView爲當前Activity設置的佈局的一部分嗎? – Pragnani 2013-04-22 18:06:39

回答

0

我認爲這個問題是在這裏:

input = (EditText) findViewById(R.id.eText); 

不能設置EditText作爲對話的內容,如果你的活動/片段佈局的一部分。

我建議你創建一個文件dialog_content.xml

<?xml version="1.0" encoding="utf-8"?> 
<EditText 
    style="@style/yourEditTextStyle" 
    android:id="@+id/myEditTextId" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 

和創造這樣的EditText

input = (EditText) LayoutInflater.from(this).inflate(R.layout.dialog_content, null); 
+0

是的,這聽起來不錯。我實際上通過從相關佈局中刪除edittext來解決我的問題,但是這個解決方案是一個更加可靠的方法......我仍然在想,爲什麼沒有辦法通過編程來改變光標顏色。 – Commander3000 2013-04-22 19:17:58

相關問題