Form Stuff | Android Developers
http://developer.android.com/intl/ja/resources/tutorials/views/hello-formstuff.html
フォーム要素のチュートリアルです。HelloFormStuffというプロジェクトを作成します。
res/layout/main.xmlを開き、以下のように変更します。
1 2 3 4 5 6 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > </ LinearLayout > |
チュートリアルページの3つの画像をローカルに保存し、res/drawable/ ディレクトリに保存します。
res/drawable/に android_button.xml を作成し、以下のように変更します。
1 2 3 4 5 6 7 8 | <? xml version = "1.0" encoding = "utf-8" ?> < selector xmlns:android = "http://schemas.android.com/apk/res/android" > < item android:drawable = "@drawable/android_pressed" android:state_pressed = "true" /> < item android:drawable = "@drawable/android_focused" android:state_focused = "true" /> < item android:drawable = "@drawable/android_normal" /> </ selector > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "10dp" android:background = "@drawable/android_button" /> </ LinearLayout > |
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 | package jp.blogspot.logroid.helloFormStuff; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class HelloFormStuffActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks Toast.makeText(HelloFormStuffActivity. this , "Beep Bop" , Toast.LENGTH_SHORT).show(); } }); } } |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "10dp" android:background = "@drawable/android_button" /> < EditText android:id = "@+id/edittext" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "text" /> </ LinearLayout > |
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 32 33 34 35 36 37 38 39 40 41 42 | package jp.blogspot.logroid.helloFormStuff; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class HelloFormStuffActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks Toast.makeText(HelloFormStuffActivity. this , "Beep Bop" , Toast.LENGTH_SHORT).show(); } }); final EditText edittext = (EditText) findViewById(R.id.edittext); edittext.setOnKeyListener( new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Perform action on key press Toast.makeText(HelloFormStuffActivity. this , edittext.getText(), Toast.LENGTH_SHORT).show(); return true ; } return false ; } }); } } |

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 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "10dp" android:background = "@drawable/android_button" /> < EditText android:id = "@+id/edittext" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "text" /> < CheckBox android:id = "@+id/checkbox" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/checkbox_label" /> </ LinearLayout > |
1 2 3 4 5 6 7 8 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World, HelloFormStuffActivity!</ string > < string name = "app_name" >HelloFormStuff</ string > < string name = "checkbox_label" >check it out</ string > </ resources > |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package jp.blogspot.logroid.helloFormStuff; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class HelloFormStuffActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks Toast.makeText(HelloFormStuffActivity. this , "Beep Bop" , Toast.LENGTH_SHORT).show(); } }); final EditText edittext = (EditText) findViewById(R.id.edittext); edittext.setOnKeyListener( new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Perform action on key press Toast.makeText(HelloFormStuffActivity. this , edittext.getText(), Toast.LENGTH_SHORT).show(); return true ; } return false ; } }); final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); checkbox.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks, depending on whether it's now checked if (((CheckBox) v).isChecked()) { Toast.makeText(HelloFormStuffActivity. this , "Selected" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HelloFormStuffActivity. this , "Not selected" , Toast.LENGTH_SHORT).show(); } } }); } } |

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 32 33 34 35 36 37 38 39 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "10dp" android:background = "@drawable/android_button" /> < EditText android:id = "@+id/edittext" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "text" /> < CheckBox android:id = "@+id/checkbox" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/checkbox_label" /> < RadioGroup android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < RadioButton android:id = "@+id/radio_red" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/red" /> < RadioButton android:id = "@+id/radio_blue" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/blue" /> </ RadioGroup > </ LinearLayout > |
1 2 3 4 5 6 7 8 9 10 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World, HelloFormStuffActivity!</ string > < string name = "app_name" >HelloFormStuff</ string > < string name = "checkbox_label" >check it out</ string > < string name = "red" >Red</ string > < string name = "blue" >Blue</ string > </ resources > |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package jp.blogspot.logroid.helloFormStuff; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; public class HelloFormStuffActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks Toast.makeText(HelloFormStuffActivity. this , "Beep Bop" , Toast.LENGTH_SHORT).show(); } }); final EditText edittext = (EditText) findViewById(R.id.edittext); edittext.setOnKeyListener( new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Perform action on key press Toast.makeText(HelloFormStuffActivity. this , edittext.getText(), Toast.LENGTH_SHORT).show(); return true ; } return false ; } }); final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); checkbox.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks, depending on whether it's now checked if (((CheckBox) v).isChecked()) { Toast.makeText(HelloFormStuffActivity. this , "Selected" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HelloFormStuffActivity. this , "Not selected" , Toast.LENGTH_SHORT).show(); } } }); final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red); final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue); radio_red.setOnClickListener(radio_listener); radio_blue.setOnClickListener(radio_listener); } private OnClickListener radio_listener = new OnClickListener() { public void onClick(View v) { // Perform action on clicks RadioButton rb = (RadioButton) v; Toast.makeText(HelloFormStuffActivity. this , rb.getText(), Toast.LENGTH_SHORT).show(); } }; } |

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "10dp" android:background = "@drawable/android_button" /> < EditText android:id = "@+id/edittext" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "text" /> < CheckBox android:id = "@+id/checkbox" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/checkbox_label" /> < RadioGroup android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < RadioButton android:id = "@+id/radio_red" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/red" /> < RadioButton android:id = "@+id/radio_blue" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/blue" /> </ RadioGroup > < ToggleButton android:id = "@+id/togglebutton" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textOn = "@string/vibrate_on" android:textOff = "@string/vibrate_off" /> </ LinearLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World, HelloFormStuffActivity!</ string > < string name = "app_name" >HelloFormStuff</ string > < string name = "checkbox_label" >check it out</ string > < string name = "red" >Red</ string > < string name = "blue" >Blue</ string > < string name = "vibrate_on" >Vibrate on</ string > < string name = "vibrate_off" >Vibrate off</ string > </ resources > |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package jp.blogspot.logroid.helloFormStuff; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; import android.widget.ToggleButton; public class HelloFormStuffActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks Toast.makeText(HelloFormStuffActivity. this , "Beep Bop" , Toast.LENGTH_SHORT).show(); } }); final EditText edittext = (EditText) findViewById(R.id.edittext); edittext.setOnKeyListener( new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Perform action on key press Toast.makeText(HelloFormStuffActivity. this , edittext.getText(), Toast.LENGTH_SHORT).show(); return true ; } return false ; } }); final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); checkbox.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks, depending on whether it's now checked if (((CheckBox) v).isChecked()) { Toast.makeText(HelloFormStuffActivity. this , "Selected" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HelloFormStuffActivity. this , "Not selected" , Toast.LENGTH_SHORT).show(); } } }); final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red); final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue); radio_red.setOnClickListener(radio_listener); radio_blue.setOnClickListener(radio_listener); final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton); togglebutton.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks if (togglebutton.isChecked()) { Toast.makeText(HelloFormStuffActivity. this , "Checked" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HelloFormStuffActivity. this , "Not checked" , Toast.LENGTH_SHORT).show(); } } }); } private OnClickListener radio_listener = new OnClickListener() { public void onClick(View v) { // Perform action on clicks RadioButton rb = (RadioButton) v; Toast.makeText(HelloFormStuffActivity. this , rb.getText(), Toast.LENGTH_SHORT).show(); } }; } |

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "10dp" android:background = "@drawable/android_button" /> < EditText android:id = "@+id/edittext" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:inputType = "text" /> < CheckBox android:id = "@+id/checkbox" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/checkbox_label" /> < RadioGroup android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < RadioButton android:id = "@+id/radio_red" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/red" /> < RadioButton android:id = "@+id/radio_blue" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/blue" /> </ RadioGroup > < ToggleButton android:id = "@+id/togglebutton" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textOn = "@string/vibrate_on" android:textOff = "@string/vibrate_off" /> < RatingBar android:id = "@+id/ratingbar" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:numStars = "5" android:stepSize = "1.0" /> </ LinearLayout > |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | package jp.blogspot.logroid.helloFormStuff; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.Toast; import android.widget.ToggleButton; public class HelloFormStuffActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks Toast.makeText(HelloFormStuffActivity. this , "Beep Bop" , Toast.LENGTH_SHORT).show(); } }); final EditText edittext = (EditText) findViewById(R.id.edittext); edittext.setOnKeyListener( new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Perform action on key press Toast.makeText(HelloFormStuffActivity. this , edittext.getText(), Toast.LENGTH_SHORT).show(); return true ; } return false ; } }); final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); checkbox.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks, depending on whether it's now checked if (((CheckBox) v).isChecked()) { Toast.makeText(HelloFormStuffActivity. this , "Selected" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HelloFormStuffActivity. this , "Not selected" , Toast.LENGTH_SHORT).show(); } } }); final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red); final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue); radio_red.setOnClickListener(radio_listener); radio_blue.setOnClickListener(radio_listener); final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton); togglebutton.setOnClickListener( new OnClickListener() { public void onClick(View v) { // Perform action on clicks if (togglebutton.isChecked()) { Toast.makeText(HelloFormStuffActivity. this , "Checked" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HelloFormStuffActivity. this , "Not checked" , Toast.LENGTH_SHORT).show(); } } }); final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar); ratingbar.setOnRatingBarChangeListener( new OnRatingBarChangeListener() { public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(HelloFormStuffActivity. this , "New Rating: " + rating, Toast.LENGTH_SHORT).show(); } }); } private OnClickListener radio_listener = new OnClickListener() { public void onClick(View v) { // Perform action on clicks RadioButton rb = (RadioButton) v; Toast.makeText(HelloFormStuffActivity. this , rb.getText(), Toast.LENGTH_SHORT).show(); } }; } |

- getAction
イベントタイプを取得する。
- setOnKeyListener
キー入力があった際のイベントリスナ登録
- setOnRatingBarChangeListener
レート変更があった際のイベントリスナ登録
0 件のコメント:
コメントを投稿