更改alertdialog中edittext的光标颜色

问题描述:

我的问题是,我在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

我认为这个问题是在这里:

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