スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書く事で広告が消せます。

[Androidアプリ開発] Buttonを使って擬似キーボード

どうもこんにちは。
昨日の擬似キーボードですが、力技で実現しました。

esp002.png


こんな感じです。
本当に力技なんですが、コード載せます。
たたみます。

.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">
<ImageView
android:id="@+id/gameLogoMainImage"
android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="5"
android:layout_marginTop="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="5"
android:gravity="center">
<TextView
android:id="@+id/gameDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:padding="5dip"
android:background="#ffffff"
android:text="input: "
android:gravity="center_vertical"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dip">
<LinearLayout
android:id="@+id/gameKeyLine1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="q"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="w"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="e"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="r"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="t"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="y"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="u"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="i"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="o"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="p"/>
</LinearLayout>
<LinearLayout
android:id="@+id/gameKeyLine2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="a"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="s"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="d"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="f"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="g"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="h"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="j"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="k"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="l"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text=";"
android:enabled="false"/>
</LinearLayout>
<LinearLayout
android:id="@+id/gameKeyLine3"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="z"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="x"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="c"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="v"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="b"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="n"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="m"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text=","
android:enabled="false"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="."
android:enabled="false"/>
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="/"
android:enabled="false"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button
android:id="@+id/gameAClearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All Clear"
android:layout_marginRight="5dip"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/gameBSpaceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BackSpace"
android:layout_margin>Right="5dip"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/gameSubmitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>



.javaファイル
public class ESPGame extends Activity implements OnClickListener {

private static final String defaultDoalog="input: ";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.espgame);
for(int i=0; i<3; i++){
LinearLayout line;
switch(i){
case 0:
line = ((LinearLayout)findViewById(R.id.gameKeyLine1)); break;
case 1:
line = ((LinearLayout)findViewById(R.id.gameKeyLine2)); break;
default:
line = ((LinearLayout)findViewById(R.id.gameKeyLine3)); break;
}
for(int j=0; j ((Button)line.getChildAt(j)).setOnClickListener(this);
}
}
((Button)findViewById(R.id.gameAClearButton)).setOnClickListener( new OnClickListener() {
public void onClick(View v) {
clearKey();
}
});
((Button)findViewById(R.id.gameBSpaceButton)).setOnClickListener( new OnClickListener() {
public void onClick(View v) {
backKey();
}
});
}

public void clickKey(Button b){
TextView text=((TextView)findViewById(R.id.gameDialog));
if((text.getText().toString().length()-defaultDoalog.length())>20){
Toast t=Toast.makeText(this, "The word is too long (over 20 letters.)", Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP, 0, 20);
t.show();
return;
}
text.setText(text.getText().toString()+b.getText().toString());
}

public void clearKey(){
((TextView)findViewById(R.id.gameDialog)).setText(defaultDoalog);
}

public void backKey(){
TextView text=((TextView)findViewById(R.id.gameDialog));
if(!(text.getText().toString().equals(defaultDoalog))){
StringBuffer sb=new StringBuffer(text.getText().toString());
sb.deleteCharAt(sb.length()-1);
text.setText(sb.toString());
}
}

public void onClick(View v) {
Button b=(Button)v;
clickKey(b);
}

}



↓流れとしてはこんな感じです。
ボタンは全部xmlファイルで用意する。
その後、LinerLayoutクラスのgetChildAtクラスを使ってforループを回し、リスナーを登録。

何かの発想の手助けになれば幸いです。

コメントの投稿

非公開コメント

Author


Author : ぞ
 ゲームをこよなく愛する重度中二病患者。ゲームとプログラミングが大好き! 決闘者だが実は遊戯王デビューしたのは大学生になってからのにわか決闘者。
 プログラマになる夢に向かって大学院で勉強中!
 ブログの記事によく出てくるマーくんというのは彼氏のことです。一緒にいろんなゲームに手を出しています。最近始めたのはヴァイスシュヴァルツとか。



pixiv
pixivにて地味に活動中!

■ぞ のページ■

カレンダー
07 | 2012/08 | 09
- - - 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 -
最新記事
最新コメント
月別アーカイブ
カテゴリ
検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QR
SAO
ソードアート・オンライン

応援中です(∩´∀`)∩
-----