直接上源码
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.graphics.drawable.ColorDrawable;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
public static ColorDrawable yellowdrawable = new ColorDrawable(0xffffff00);
public static ColorDrawable whitedrawable = new ColorDrawable(0xffffffff);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setBackgroundDrawable(whitedrawable);
}
public boolean onTouchEvent(MotionEvent event) {
LinearLayout l1;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
this.getWindow().setBackgroundDrawable(yellowdrawable);
break;
case MotionEvent.ACTION_UP:
this.getWindow().setBackgroundDrawable(whitedrawable);
break;
}
return super.onTouchEvent(event);
}
}
其实关键代码只有this.getWindow().setBackgroundDrawable(一句,如果只想改变背景色的话不用扯什么selector。
这样的效果是触摸屏幕时背景色为黄,抬起手指恢复为白。
--------------------------一条分割线------------------------------
再附上将界面设为白底黑字的方法:
在main.xml的TextView和AndroidManifest.xml的activity标签里加入
android:theme="@android:style/Theme.Light"
--------------------------一条分割线------------------------------
最后说一下,笔者的开发环境是intellij13,不是eclipse。不过程序应该没有太大区别,最多是package和import部分不一样。
intellij是个不错的java开发环境,代码分析上比eclipse要智能。缺点是占内存。
EOB