简体   繁体   English

设置EditText的文本颜色

[英]Setting text color of EditText

I have a dialog that triggers on EditText's context menu. 我有一个对话框,可在EditText的上下文菜单上触发。 Dialog sets the amount of red, green and blue of EditText. 对话框设置EditText的红色,绿色和蓝色的数量。 The problem is that runtime error occurs when I click positive button. 问题是当我单击肯定按钮时发生运行时错误。 Items above works fine, they set the color of text in EditText as it should, but rgbDialog don't. 上面的项目工作正常,它们可以按需设置EditText中文本的颜色,而rgbDialog则不能。

I think that the problem occurs here: 我认为问题发生在这里:

            etRed = (EditText)findViewById(R.id.etRed);
            etGreen = (EditText)findViewById(R.id.etGreen);
            etBlue = (EditText)findViewById(R.id.etBlue);

But I don't know how to initialize etRed, etGreen and etBlue right. 但是我不知道如何正确初始化etRed,etGreen和etBlue。 I think that I should place something before findView like 我认为我应该在findView之前放置一些东西

etRed = (EditText)Dialog.findViewById(R.id.etRed);

But don't know what, because everything is happening in the same method (onContextItemSelected) 但是不知道是什么,因为一切都在同一方法中发生(onContextItemSelected)

 @Override
public boolean onContextItemSelected(MenuItem item) {

    switch(item.getItemId()){
    case R.id.white:
        temp.setTextColor(Color.WHITE);
        break;
    case R.id.red:
        temp.setTextColor(Color.RED);
        break;
    case R.id.green:
        temp.setTextColor(Color.GREEN);
        break;
    case R.id.rgb:

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("choose the amount of red green and blue");

        LayoutInflater inflater = this.getLayoutInflater();

        dialogBuilder.setView(inflater.inflate(R.layout.colors, null));

        dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                etRed = (EditText)findViewById(R.id.etRed);
                etGreen = (EditText)findViewById(R.id.etGreen);
                etBlue = (EditText)findViewById(R.id.etBlue);

                red=etRed.getText().toString();
                green = etGreen.getText().toString();
                blue = etBlue.getText().toString();

                intRed = Integer.parseInt(red);
                intGreen = Integer.parseInt(green);
                intBlue = Integer.parseInt(blue);



                temp.setTextColor(Color.rgb(intRed, intGreen, intBlue));



            }
        });

        dialogBuilder.setNegativeButton("CANCEL", null);

        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        break;
        default:
            break;
    }

    return super.onContextItemSelected(item);

}

EditText temp is set like this, and works fine for items above rgb 像这样设置EditText temp,并且对于rgb以上的项目效果很好

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {


        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context, menu);

        temp = (EditText)findViewById(v.getId());

        super.onCreateContextMenu(menu, v, menuInfo);
    }

you can have something like below to call findViewById from correct layout: 您可以像下面这样从正确的布局调用findViewById:

View dialogView = inflater.inflate(R.layout.colors, null);
dialogBuilder.setView(dialogView );

and when you are calling findViewById in onClick you can have like: 当您在onClick中调用findViewById时,您可以像这样:

etRed = (EditText)dialogView.findViewById(R.id.etRed);
                etGreen = (EditText)dialogView.findViewById(R.id.etGreen);
                etBlue = (EditText)dialogView.findViewById(R.id.etBlue);

Check if this helps. 检查是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM