忍者ブログ

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ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

本日はカスタムダイアログの(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回]

PR

Post your Comment Post your Comment
Name
Title
E-mail
URL
Comment
Pass   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
プラグインタイトル カレンダー
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]