import java.util.ArrayList;import com.tech.adpter.LauncherAdpter;import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;public class LauncherActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        down();
        initIconSmall();
        initIconLarge();
        initName();
       
    }    private View view;
    private GridView grid;
    private LauncherAdpter ladpter;
    private int foucs ;
    private void down(){
     LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.down, null);
     grid = (GridView) view.findViewById(R.id.down_launcher);
     grid.setFocusable(true);
     ladpter = new LauncherAdpter(this);
     grid.setAdapter(ladpter);
     grid.setSelection(2);
     foucs = grid.getSelectedItemPosition();
     LauncherAdpter.current = foucs;
     setContentView(view);
     System.out.print(foucs);
    
    }
    
    public static ArrayList<Bitmap> bitmap = new ArrayList<Bitmap>();
    private void initIconSmall(){
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tv);
     Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.multimedia);
     Bitmap bmp3 = BitmapFactory.decodeResource(getResources(), R.drawable.allapps);
     Bitmap bmp4 = BitmapFactory.decodeResource(getResources(), R.drawable.setting);
     Bitmap bmp5 = BitmapFactory.decodeResource(getResources(), R.drawable.game);
     bitmap.add(bmp);
     bitmap.add(bmp2);
     bitmap.add(bmp3);
     bitmap.add(bmp4);
     bitmap.add(bmp5);
    }
    
    public static ArrayList<Bitmap> bit = new ArrayList<Bitmap>();
    private void initIconLarge(){
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tv_large);
     Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.multimedia_large);
     Bitmap bmp3 = BitmapFactory.decodeResource(getResources(), R.drawable.allapps_large);
     Bitmap bmp4 = BitmapFactory.decodeResource(getResources(), R.drawable.setting_large);
     Bitmap bmp5 = BitmapFactory.decodeResource(getResources(), R.drawable.game_large);
     bit.add(bmp);
     bit.add(bmp2);
     bit.add(bmp3);
     bit.add(bmp4);
     bit.add(bmp5);
    }
    
    public static ArrayList<Integer> name = new ArrayList<Integer>();
    private void initName(){
     name.add(R.string.tv);
     name.add(R.string.media);
     name.add(R.string.apps);
     name.add(R.string.settings);
     name.add(R.string.game);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){     switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
grid.setFocusable(true);
foucs--;
if(foucs<0){
foucs=4;
}
LauncherAdpter.current = foucs;
grid.setAdapter(ladpter);
System.out.println(foucs);
grid.setSelection(foucs);
return false;
case KeyEvent.KEYCODE_DPAD_RIGHT:
grid.setFocusable(true);
foucs++;
if(foucs>4){
foucs=0;
}
LauncherAdpter.current = foucs;
grid.setAdapter(ladpter);
grid.setSelection(foucs);
System.out.println(foucs);
return false;
case KeyEvent.KEYCODE_DPAD_CENTER:
if(foucs == 0){
packName = "";
className = "";
startActivity();
break;
}else if(foucs ==1){
packName = "";
className = "";
startActivity();
break;
}else if(foucs == 2){
packName = "";
className = "";
startActivity();
break;
}else if(foucs == 3){
packName = "com.android.settings";
className = "Settings";
startActivity();
break;
}else if(foucs == 4){
packName = "";
className = "";
startActivity();
break;
}
return false;
}
     return true;
    }
    
    private String packName;
    private String className;
    public void startActivity(){
     ComponentName comName = new ComponentName(packName,className);
Intent intent = new Intent();
intent.setComponent(comName);
startActivity(intent);
    }
}

解决方案 »

  1.   

    还有按center时,一点响应都没有,按“↑”和“↓”每次都有响应....
      

  2.   

    描述不清楚,代码无标注....建议方法:
    在每个case里面加上Log.i("Test", "对应的keyCode");来判断到底是没响应呢?还是有响应,但是响应处理的代码有误。
      

  3.   

    哇,一点多都还没睡。那个标注的话down()是布局底部的界面,initIconSmall();initIconLarge();initName();分别来初始化一些需要用的图片和对应名字。主要是那个onkeydown()里KeyEvent.KEYCODE_DPAD_LEFT 来监听左键按下“←”事件,KeyEvent.KEYCODE_DPAD_RIGHT来监听右键按下“→”事件,KeyEvent.KEYCODE_DPAD_CENTER是按下“确定”后的事件。加上Log.i()看到我按“←”“→”的时候都是间隔几下才响应,“确定”键不响应,不是响应后处理有问题。
      

  4.   

    难道是怪我不该用onkeydown()?