程序的目录结构:
MainActivity代码;
package com.mth.flashlight;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button red = (Button) findViewById(R.id.red);
Button blue = (Button) findViewById(R.id.blue);
Button green = (Button) findViewById(R.id.green);
red.setOnClickListener(this);
blue.setOnClickListener(this);
green.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.red:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
break;
case R.id.blue:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLUE));
break;
case R.id.green:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
break;
}
}
}