初歩的なところでハマる

おもむろにAndroidプログラム作成中にハマった事。
とりあえずHelloWorldなプロジェクトから、Buttonをxmlでレイアウトに記述した。idも設定して、
ソースコードからuttonを取得した。
↓こんな感じで

Button btn = (Button)findViewById(R.id.button);
btn.setText("hoge_button");

そしたら、何故か落ちるプログラム。

↓が今回のレイアウト。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
	
	<Button
		android:text="button"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		id="@+id/button"/>
	
</LinearLayout>

最初何がおかしいのか分からなかった…
しかし、よーく見るとIDの設定が

	<Button
		android:text="button"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		id="@+id/button"/>

android: が抜けてた為にIDが空だったため、
そもそもfindViewById()出来てなかったという。

っていうか、こんな記述通るんだね…勉強になった。

超手軽なBT キーボード(トラックパッド付き)

便利そうな入力デバイスの出会い

いつものようにお世話になっているAmazonを眺めていたら
こんな商品が目に止まる…

正直買うかどうか悩んだが、ポチっとするのに時間はかからなかったw

自宅に到着

そして、相変わらずの翌日には到着。
クロネコさん、いつも頭が下がりますm(_ _)m

f:id:djkouryou:20170923214705j:plain

パッケージには一切型番が書いてませんでしたw
説明書に書いてあった『EW-RB05』が型番ですね。
※ちなみに、EWINというメーカーのようですが、
HPには製品情報が載っておらず…
ダイジョウブナノカ?

なんちゃって製品情報

開封後
f:id:djkouryou:20170923215534j:plain

バックライト付き!
f:id:djkouryou:20170923215450j:plain

他は割愛しますが、充電式なのはありがたいです。
※ただし充電ポートは、USBmini
充電をすませて、手持ちのAndroid端末とペアリング。
IMEATOKを利用、実はハードウェアキーもサポートするIMEです。
Androidはマウスもサポートする為、トラックパッドも使えるのですよ!

机上でタッチタイプよりも両手で持ち、親指で打つスタイルがこの子のスタンダードみたいですね。
※思ったより打鍵感は良いですね。親指で押すことを考慮すると、ちょっと重めです。カチカチよりプチプチ?カーソルキーはカチカチ。

Shiftキーが左にしかないのでキー配置左側のキーは、
たとえば"!"を打つとき「左親指シフトキー+右親指1キー」わりかししんどいです。

重複しますが、打鍵感はかなり気持ちいいので長文もそこまで苦にはならないですね~
※この記事もAndroidスマホ(covia g07)とEW-BR05で書いてます。
f:id:djkouryou:20170923221322j:plain

LayoutInflarer

LayoutInflaterをちょっと使ってみた。
それぞれ追記分だけ記述。
Layoutの親ビューはLinearLayout(vertid 

main.xml

プロジェクト作成で自動生成されるmainのレイアウトに、

ボタンとスクロールビューを足した。

中程にあるLinearLayout(id=inflateScreen)にinflateしたViewを表示させる。

Buttonのメソッド名はactivateInflaterとした。

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Layout Inflate!!!"
        android:textSize="20dp"
        android:onClick="activateInflater"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/inflateScreen"/>
    </ScrollView>

 

inflater_layout.xml 

インフレーター用レイアウトファイル。

とりあえず TextView他ウィジェットを適当にレイアウトしてみた。

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Inflate TextView"
        android:textSize="36dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <DigitalClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <AnalogClock
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

MainActivity.java
ソースコード

 >|Java|

  //プライベート領域で変数宣言

  private LinearLayout inflateScreen;

 

//onCreate内でfindViewById()して、実体を取得

inflateScreen = 
        (LinearLayout)findViewById(R.id.inflateScreen);

 

public void activateInflater(View view){
    LayoutInflater layoutInflater = getLayoutInflater();
    View inflateView = 
            layoutInflater.inflate(R.layout.inflate_layout,null);
    inflateScreen.addView(inflateView);
}

|

RadioGroupとRadioButton

使えるかどうか分からないけど、
サンプルプログラム弄ってて気づいた。

<!--Layout-->
<RadioGroup
android:id="@+id/my_group"
属性省略...>
     <RadioButton
             android:text="hoge"
             属性省略.../>
     <RadioButton
             android:text="foo"
             属性省略.../>
</RadioGroup>

と、RadioGroupだけidつけた状態で、
RadioButtonを取得するのに...

RadioGroup group = (RadioGroup)findViewById(R.id.my_group);

//グループ内でどのボタンもチェックされてない場合-1が返る
int buttonId = group.getCheckedRadioButtonId();

RadioButton button = (RadioButton)findViewById(buttonId);

これで取得できる。後はゲットしたRadioButtonを使って煮るなり焼くなりお好きなように...
上の例では、返り値が-1になるのは考慮していないので、注意!

スマホからテスト

アプリを利用して書いてみるテスト。

 

 いろいろテストしながら。