忍者ブログ

Androidでアプリの開発をしていきます。まだ初心者。。。 JavaはOJC-Pを取得しました。 無事Androidの資格もGETです

<< 09  2024/10  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    11 >>

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

今日は国際化について少しメモしておきます。
せっかくなら、日本のみならず世界で!という野望があるので
せっせと国際化に対応させてみます。

res/values : デフォルトを英語対応
res/values-ja : デバイス日本語選択時に日本語

このように設定しました。
デフォルトを英語にしなくちゃいけないのは、日本語がデフォルトになっていると英語圏以外が
すべて日本語で表示されてしまうためだそうです。
勉強になります






拍手[0回]

PR

実際にスマートフォンを使っていて、ひどい悩んでいたのが、
内蔵メモリの少なさゆえに、インストールが内蔵メモリにされてしまう場合です。
いまは内蔵メモリも大きい端末が多いですが、昔はそうでもなく、
内蔵メモリにインストールされてしまうアプリを泣く泣くアンインストールしていた経験があります。

Android2.2からはアプリケーションをSDメモリカードにインストールができるように設定することが
可能になりましたので、今回はその方法をメモします。

①AndroidManifest.xmlの<manifest>タグに以下を追加する

android:installLocation="preferExternal"

これでOK。
ちなみに選択できる値としては、

(1)internalOnly:内蔵メモリへのインストールのみを許可
(2)auto:内蔵メモリ優先でインストールする。内蔵メモリがいっぱいならSDメモリカードへインストール
(3)preferExternal:SDメモリカード優先でインストールする。SDメモリカードがいっぱいなら内蔵メモリへインストール

と、なります。簡単ですね







拍手[0回]

個人的にメモしたいことが増えてしまい、連続投稿致します。
今回はListViewの作成についてです。
ListViewは何かと作成する機会が今後もあるでしょうし、ここにメモしておきます。

①まず枠組みとなるレイアウトの作成
res/layout/layout_main.xml←デフォルト作成のレイアウトファイル


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" >
 
↓入力欄と保存ボタンの作成を行っています、TextViewは追加項目が発生していない場合のみ表示 
    <EditText 
        android:layout_height="wrap_content"
        android:layout_width="350dp"
        android:hint="@string/hintTxtItem"
        android:inputType="text"
        android:id="@+id/txtItem" />
    <Button 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/btnAdd"
        android:layout_toRightOf="@id/txtItem"
        android:text="@string/lblBtnAdd" />
    <TextView 
        android:id="@android:id/empty"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal"
        android:text="@string/txtEmpty"
        android:layout_below="@id/txtItem" />

↓ListViewの作成です。ここではマルチ選択を設定しています
    <ListView 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@android:id/list"
        android:layout_below="@id/txtItem"
        android:choiceMode="multipleChoice" >        
    </ListView>

android:idの種類はこちらのサイト様を参考にしました。ありがとうございます。
http://androyer.blogspot.jp/2011/05/androidid.html   
あんまり意識してなかったのと、資格取得にも意味についてまでは語られなかったので
とても参考になりました。   


↓削除ボタンの作成
 <Button 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/btnDel"
        android:text="@string/lblBtnDel"
        android:layout_alignParentBottom="true" />
  
</RelativeLayout>

ちなみに@stringはres/values/string.xmlで設定したリソースを使用しています。
直入力でもいいのですが、国際化や今後のためにもリソースからの参照がいいと思います。
個人的に。


②次にActivityの設定を行います。

import java.util.ArrayList;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

↓ここで1つ、当たり前かもしれませんが、必ず継承はListActivityを継承します。
ここで既存のまま、Activityクラスの継承をしているとのちのgetView()が使用できません。
(管理人はここでしばらく躓きました……orz)
public class MainActivity extends ListActivity {
 
ArrayList list = new ArrayList();
ArrayAdapter adapter;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);

↓ボタンの適用とArrayAdapterの設定
Button btnadd = (Button)findViewById(R.id.btnAdd);
Button btndel = (Button)findViewById(R.id.btnDel);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,list);

↓いわゆる保存ボタンを押したときの処理を実装
OnClickListener listener = new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
EditText edit = (EditText)findViewById(R.id.txtItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
 
}
};
↓いわゆる削除ボタンを押したときの処理を実装
OnClickListener listenerDel = new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
 
for(int i = itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
 
}
};
↓ボタンに処理を実装しています
btnadd.setOnClickListener(listener);
btndel.setOnClickListener(listenerDel);
setListAdapter(adapter);
}
}
 
とまあ、こんな具合で入力したことを保存ボタンでリストに追加し、
チェックボタンで選択して、削除ボタンでそれらを削除するという処理を実装しました。

完成は下になります。



あくまで、一例ですので、ご参考までに。





拍手[0回]

本日はカスタムダイアログの(EditText)ありについてご説明致します。
まあ、そんなのわかるよ!って人は読み飛ばしてもOKです。

①「res/layout」でカスタムダイアログ用のレイアウトファイルを作成
ここでは「dialog.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" >
    
    <EditText 
        android:id="@+id/Text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>

赤色のところが入力する場所です。
次に、表示画面からカスタムダイアログを表示する簡単なボタンを作成します。

②res/layout/layout_main.xml←これはデフォルトで作成されるレイアウトファイル


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
    <Button 
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/CuLa"/>
 
</RelativeLayout>

赤色のコードを入れることで簡単なボタンを作成しました。
android:text="@string/Cula"は「res/values/string.xml」で設定してある文字リソースを使用しています。

③次にActivityのコードを作成します。
src/パッケージ名/MainActivity.java←デフォルトで作成されるファイルをいじります。


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);

↓ボタンの適用をOnCliclistenerに行っている
((Button)findViewById(R.id.button)).setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
↓さきほど作成したカスタムダイアログ用のレイアウトファイルとEditTextを適用
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.dialog, null);
final EditText editText = (EditText)view.findViewById(R.id.Text1);

↓カスタムダイアログの作成
new AlertDialog.Builder(MainActivity.this)
.setTitle("Hello,AlertDialog!") //タイトルを決める
.setIcon(R.drawable.ic_launcher) //アイコンの設定
.setView(view) //カスタムダイアログの表示

↓いわゆるOKボタンを作成
.setPositiveButton("YES", new DialogInterface.OnClickListener() {

↓本当はOKボタンを押したときの動作を追加するんですが、今回は作成のみということで
@Override
public void onClick(DialogInterface dialog,int which) {
}
 
})
↓同じくいわゆるNOボタンの作成(ここではNOボタンを選択するとnullで終了するようにしています)
.setNegativeButton("NO",null)
.show();
}
});}}

たったこれだけで、自分ごのみの入力用ダイアログが完成です。
あとはアイコンだったり、レイアウトだったりをいじくったり、
処理を実装すればOKです。完成は下。


ボタンを押すと、



という具合に作成される。
ここではもちろんOKボタンを押しても意味なしです。NOボタンも同じく


ではまた^^


拍手[0回]

前回に引き続き、オリジナルバーの作成です。

[レイアウトファイル.xml]


↓全体のレイアウトを設定

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

↓タイトルバーのレイアウトを設定
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

↓タイトルバーの内部レイアウトを設定
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="#666666"
            android:gravity="center|right"
            android:orientation="horizontal" >
↓タイトルバーの部品(テキスト)
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="5dip"
                android:paddingLeft="10dip"
                android:paddingRight="85dip"
                android:paddingTop="5dip"
                android:text="Title"
                android:textColor="#F7F7F7"
                android:textSize="15sp"
                android:textStyle="bold" />

↓タイトルバーの部品(イメージボタン)
            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:src="@drawable/add" />
 
            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:src="@drawable/delete" />
        </LinearLayout>



出来上がったのが、上記。
まあ、シンプルですが、これを代用すればなかなかにいろいろ作れるんではないでしょうか?
うーん。まだまだ
 
 

拍手[0回]

プラグインタイトル カレンダー
09 2024/10 11
S M T W T F S
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
プラグインタイトル 最新CM
プラグインタイトル プロフィール
HN:
Hideaki
性別:
非公開
自己紹介:

プラグインタイトル バーコード
プラグインタイトル ブログ内検索
プラグインタイトル ブック検索
プラグインタイトル 訪問者数
Script:Ninja Blog  Design by: タイムカプセル
忍者ブログ [PR]