0

我正在尝试写一个文字游戏。这就是为什么在某些时候我需要在虚拟键盘上按下按键。我需要将这封信写到 textView。不应使用 EditText。我完全被听众等困住了。

任何帮助,将不胜感激。

public class Oyun extends Activity implements OnKeyboardActionListener
{

@Override
public void onCreate(Bundle savedInstanceState)
{
    overridePendingTransition(R.anim.push_left_in,R.anim.push_up_out);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.oyun);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    in = this.getResources().openRawResource(R.raw.source);
    reader = new BufferedReader(new InputStreamReader(in));

    sayac  = (TextView)findViewById(R.id.counterDown);
    soru = (TextView)findViewById(R.id.Soru);
    answerButton = (ImageButton)findViewById(R.id.CevapButon);
    harfAlButton = (ImageButton)findViewById(R.id.HarfAlButon);
    leyat  = (LinearLayout)findViewById(R.id.leyat);

    final MyCounter timer = new MyCounter(240000, 1000);
    timer.start();

    try
    {
        bringQuestion(reader, in);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }




    harfAlButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {   
            harfAl();
            if(alinanHarfSayisi == uzunluk)
            {
                alinanHarfSayisi = 0;
                   try
                    {
                        bringQuestion(reader, in);
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
            }
        }

    }); 

    answerButton.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            letter[5].setVisibility(View.INVISIBLE);
            if(keyCode==KeyEvent.KEYCODE_A)
            {
                letter[1].setText("A");
            }
            return false;
        }
    });

    answerButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {   

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

                    /*try
                    {
                        bringQuestion(reader, in);

                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }*/     
        }

    }); 
}


public int harfAl()
{
    alinanHarfSayisi++;
    rand = rastgeleHarf.nextInt(uzunluk);

    while(letter[rand].getText() != "")
    {
        rand = rastgeleHarf.nextInt(uzunluk);
    }

    desired = cevap.charAt(rand);
    letter[rand].setText(""+ desired);
    return alinanHarfSayisi;    
}

public class MyCounter extends CountDownTimer
{

    public MyCounter(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished)
    {
        long minutes = millisUntilFinished/60000;
        long rSeconds = millisUntilFinished%60000;
        long seconds = rSeconds/1000;

        if(seconds>=10)
        {
            sayac.setText(minutes + ":" + seconds);
        }
        else
        {
            sayac.setText(minutes + ":0" + seconds);
        }

    }

    @Override
    public void onFinish()
    {


    }
}



public void bringQuestion(BufferedReader rea, InputStream inputS) throws IOException
{       
    if (inputS!=null)
    {                           
        if ((line = rea.readLine()) != null)
        {   
            soru.setText(line);
            cevap = rea.readLine();     
        }
    }       
    //is.close();       
    for(int i = 0; i<uzunluk; i++)
    {
        letter[i].setVisibility(View.GONE);
    }

    uzunluk = cevap.length();

    letter = new TextView[uzunluk];
    for(int i = 0; i<uzunluk; i++)
    {
        letter[i]=new TextView(this);
        letter[i].setBackgroundResource(R.drawable.shape);
        letter[i].setGravity(17);
        letter[i].setTextColor(Color.BLACK);
        letter[i].setTextSize(20);
        letter[i].setTypeface(Typeface.SERIF, Typeface.BOLD);
        letter[i].setText("");
        this.leyat.addView(letter[i]);
    }   
}


@Override
public void onKey(int primaryCode, int[] keyCodes)
{
    if(primaryCode==KeyEvent.KEYCODE_A)
    {
        letter[1].setText("B");
    }

}


@Override
public void onPress(int primaryCode)
{
    if(primaryCode==KeyEvent.KEYCODE_A)
    {
        letter[1].setText("C");
    }

}


@Override
public void onRelease(int primaryCode) {
    // TODO Auto-generated method stub

}


@Override
public void onText(CharSequence text) {
    // TODO Auto-generated method stub

}


@Override
public void swipeDown() {
    // TODO Auto-generated method stub

}


@Override
public void swipeLeft() {
    // TODO Auto-generated method stub

}


@Override
public void swipeRight() {
    // TODO Auto-generated method stub

}


@Override
public void swipeUp() {
    // TODO Auto-generated method stub

}

}
4

3 回答 3

0

如果你真的想捕捉击键,你可以使用 OnKeyListener。按照这个链接 在这里

于 2012-06-27T13:17:34.473 回答
0

KeyEvent对象应该为您提供所需的功能。

于 2012-06-27T13:12:03.380 回答
0

嘿,请遵循以下代码:-

addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
        {
            switch (keyCode)
            {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    addCourseFromTextBox();
                    return true;
                default:
                    break;
            }
        }
        return false;
    }
});
于 2012-06-27T13:48:28.887 回答