Android WebKit专注于textarea不会绘制背景图像

Cla*_*nda 11 css android webkit focus background-image

Android WebKit在textareas上绘制白色背景+边框,并在有焦点时输入.我如何阻止它这样做?

没有焦点文本区域的背景图像

我试过改变-webkit-tap-highlight-color,-webkit-appearance,-webkit-backface-visibility,outline和一些其他的东西,似乎没什么用.救命!

cai*_*ci2 8

编辑

好的,所以忘记我以前回答的一切.我已经运行了一些测试,这似乎只是Android 4.0.3上的一个问题.还有一些坏消息.

我开始深入挖掘源代码,我相信问题出在WebTextView类上.处理textviews和textareas的类.

如果你在第233-240行看到绘制背景的方法总是在里面绘制一个蓝色矩形和一个白色矩形.这正是您的屏幕截图显示的内容.

一个好的解决方案是扩展这个类并修复它.不幸的是,sdk中没有WebTextView类.意思是你无法覆盖它.

我尝试了各种各样的javascript来改变textarea颜色,甚至重叠元素,但它没有帮助.这不是我们可以从HTML代码中获得的东西.

所以你无法在java上修复它,你无法在javascript上修复它.我担心这个问题没有解决办法.

编辑

我纠正了.感谢@grandstaish的焦点建议,我们可以在TextView创建后轻松更改背景颜色.我试图使用ViewTreeObserver一个优雅的解决方案,但我不能只针对我想要的观点.每个TextView都有一个透明的背景,我不希望这样.所以混合方法解决了它:

HTML:

 <div style="background:red; position:relative; width:100%; height:500px; z-index:0;" id='behind' >
    <textarea name="Name"style="background:green; border:none; height:100px; width:250px; position:absolute; top:0; left:0; z-index:1;" 
            id='area1' onFocus='runOnFocus()' ></textarea>
    </div>
Run Code Online (Sandbox Code Playgroud)

JS:

function runOnFocus(){
if (window.device.platform == "Android" && window.device.version =="4.0.3")
//I had to set a timeout here or this would trigger before the TextView was created. 1ms seems to be enough though.
window.setTimeout(function(){
        window.AndroidLayer.setBackground();},1);        
}
Run Code Online (Sandbox Code Playgroud)

Java的:

private String TAG = "TextArea";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/index.html");
        appView.addJavascriptInterface(this, "AndroidLayer");                       
    }

public void setBackground(){
    runOnUiThread( new Runnable() {         
        @Override
        public void run() {
            Log.d("native","gotFocus");
            View tv = (View) appView.getFocusedChild();     
            if (tv == null)
                Log.d("native","isNull");
            else
                tv.setBackgroundColor(Color.TRANSPARENT);               
        }
    });

}
Run Code Online (Sandbox Code Playgroud)

因此,我们可以控制页面上的哪些元素将触发背景更改.我找不到任何不必要的副作用,但我没有时间在所有版本中测试它,所以我选择将其限制为4.0.3,这是我能找到的唯一版本.