我正在尝试写一个文字游戏。这就是为什么,在某些时候我需要在虚拟键盘上按下按键。我需要把这封信写成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
}
}
答案 0 :(得分:0)
KeyEvent
对象应该为您提供所需的功能。
答案 1 :(得分:0)
如果你确实想捕捉击键,你可以使用OnKeyListener。点击此链接 Here
答案 2 :(得分: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;
}
});