android studying
DoDos Blog
1、按键改变背景颜色
//MainActivity.java
package com.xiaod.hello;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tvHello = (TextView) findViewById(R.id.tvHello);
Button btnRed = (Button) findViewById(R.id.btnRed);
Button btnGreen = (Button) findViewById(R.id.btnGreen);
btnRed.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
tvHello.setText("click");
tvHello.setBackgroundColor(Color.RED);
}
});
btnGreen.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
tvHello.setText("click");
tvHello.setBackgroundColor(Color.GREEN);
}
});
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="TextView"
android:id="@+id/textAlphaValue"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TextView>
<ImageView android:id="@+id/image"
android:src="@drawable/laopo"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ImageView>
</LinearLayout>
2、上下左右按键改变图片透明度
//main.java
package com.xiaod.keyevent;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
private TextView mAlphaValueText;
private int mAlaphaValue;
private ImageView mImage;
private static final String TAG = "KeyEvent";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAlphaValueText = (TextView) findViewById(R.id.textAlphaValue);
mAlaphaValue = 900;
mImage = (ImageView) findViewById(R.id.image);
mImage.setAlpha(mAlaphaValue);
mAlphaValueText.setText("Alpha = " + mAlaphaValue*100/0xff + "%");
}
public boolean onKeyDown(int keyCode, KeyEvent msg) {
Log.v(TAG, "onKeyDown: keyCode = " + keyCode);
Log.v(TAG, "onKeyDown: String = " + msg.toString());
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_RIGHT:
mAlaphaValue += 10;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_DOWN:
mAlaphaValue -= 10;
break;
default:
break;
}
if(mAlaphaValue > 0xFF)
mAlaphaValue = 0xFF;
if(mAlaphaValue < 0x0)
mAlaphaValue = 0x0;
mImage.setAlpha(mAlaphaValue);
mAlphaValueText.setText("Alpha = " + mAlaphaValue*100/0xff + "%");
return super.onKeyDown(keyCode, msg);
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="TextView"
android:id="@+id/textAlphaValue"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TextView>
<ImageView android:id="@+id/image"
android:src="@drawable/laopo"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ImageView>
</LinearLayout>
补充:移动开发 , Android ,