Andriod 4個按鈕事件範例 KeyDown onClick
Andriod 4個按鈕事件範例 KeyDown onClick
自行撰寫4個按鈕事件範例,歡迎有興趣的同好,藝起來C/P一下。
package com.example.activity_keyevent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
///////////////////////////////////////
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
public class MainActivity extends Activity {
Button[] bt;
int gintnowfouce;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/////////////////////////////////////////
AbsoluteLayout AbsLayout1=new AbsoluteLayout(this);
setContentView(AbsLayout1);
/////////////////////////////////////////
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
bt=new Button[4];
bt[0]=new Button(this);
bt[0].setText("0");
bt[0].setWidth(metrics.widthPixels/2);
bt[0].setHeight(metrics.heightPixels/2);
bt[0].setFocusable(true);
bt[0].setOnClickListener(new ButListener());
gintnowfouce=0;
bt[0].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, 0 ) );
AbsLayout1.addView(bt[0]);
bt[1]=new Button(this);
bt[1].setText("1");
bt[1].setWidth(metrics.widthPixels/2);
bt[1].setHeight(metrics.heightPixels/2);
bt[1].setOnClickListener(new ButListener());
bt[1].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, 0 ) );
AbsLayout1.addView(bt[1]);
bt[2]=new Button(this);
bt[2].setText("2");
bt[2].setWidth(metrics.widthPixels/2);
bt[2].setHeight(metrics.heightPixels/2);
bt[2].setOnClickListener(new ButListener());
bt[2].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, metrics.heightPixels/2 ) );
AbsLayout1.addView(bt[2]);
bt[3]=new Button(this);
bt[3].setText("3");
bt[3].setWidth(metrics.widthPixels/2);
bt[3].setHeight(metrics.heightPixels/2);
bt[3].setOnClickListener(new ButListener());
bt[3].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, metrics.heightPixels/2 ) );
AbsLayout1.addView(bt[3]);
/////////////////////////////////////////
}
public boolean onKeyDown(int keycode, KeyEvent e)
{
String str;
str="";
switch(keycode)
{
case KeyEvent.KEYCODE_DPAD_UP:
switch(gintnowfouce)
{
case 0:
gintnowfouce=3;
break;
case 1:
gintnowfouce=4;
break;
case 2:
gintnowfouce=1;
break;
case 3:
gintnowfouce=2;
break;
}
str = "上"; break;
case KeyEvent.KEYCODE_DPAD_DOWN:
switch(gintnowfouce)
{
case 0:
gintnowfouce=3;
break;
case 1:
gintnowfouce=4;
break;
case 2:
gintnowfouce=1;
break;
case 3:
gintnowfouce=2;
break;
}
str = "下"; break;
case KeyEvent.KEYCODE_DPAD_LEFT:
switch(gintnowfouce)
{
case 0:
gintnowfouce=2;
break;
case 1:
gintnowfouce=1;
break;
case 2:
gintnowfouce=4;
break;
case 3:
gintnowfouce=3;
break;
}
str = "左"; break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
switch(gintnowfouce)
{
case 0:
gintnowfouce=2;
break;
case 1:
gintnowfouce=1;
break;
case 2:
gintnowfouce=4;
break;
case 3:
gintnowfouce=3;
break;
}
str = "右"; break;
case KeyEvent.KEYCODE_DPAD_CENTER:
str = "中央"; break;
case KeyEvent.KEYCODE_ENTER:
str = "ENTER"; break;
default:
this.finish();
}
//tv.setText("��" + str + "。");
bt[0].setFocusable(false);
bt[1].setFocusable(false);
bt[2].setFocusable(false);
bt[3].setFocusable(false);
bt[gintnowfouce].setFocusable(true);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ButListener implements OnClickListener
{
public void onClick(View v)
{
if(v==bt[0])
{
bt[0].setText("0 clicked...");
}
if(v==bt[1])
{
bt[1].setText("1 clicked...");
}
if(v==bt[2])
{
bt[2].setText("2 clicked...");
}
if(v==bt[3])
{
bt[3].setText("3 clicked...");
}
}
}
}
|