2010년 4월 12일 월요일

Form configuration components

1) /HelloFormStuff/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<ImageButton
    android:id="@+id/android_button"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:src="@drawable/android" />  
<EditText
    android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>  
<CheckBox android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="check it out" />
<RadioGroup
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
 
  <RadioButton android:id="@+id/radio_red"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Red" />
 
  <RadioButton android:id="@+id/radio_blue"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Blue" />
</RadioGroup>
<ToggleButton android:id="@+id/togglebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Please select a planet:"
    />
<Spinner android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawingCacheQuality="high"
        android:prompt="@string/planet_prompt"
    />
</LinearLayout>

2) /HelloFormStuff/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloFormStuff!</string>
    <string name="app_name">HelloFormStuff</string>
    <string name="planet_prompt">Choose a planet</string>
</resources>

3) /HelloFormStuff/res/values/arrays.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <string-array name="planets">
        <item>Chelsea</item>
        <item>ManUtd</item>
        <item>Arsenal</item>
        <item>ManCity</item>
        <item>Tottenham</item>
        <item>Liverpool</item>
        <item>AstonVilla</item>
        <item>Everton</item>
    </string-array>
</resources>

4) /HelloFormStuff/src/my/HelloFormStuff/HelloFormStuff.java
package my.HelloFormStuff;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.KeyEvent;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class HelloFormStuff extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final ImageButton button = (ImageButton) findViewById(R.id.android_button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(HelloFormStuff.this, "I love Liverpool", Toast.LENGTH_LONG).show();
            }
        });
        
        final EditText edittext = (EditText) findViewById(R.id.edittext);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  // Perform action on key press
                  Toast.makeText(HelloFormStuff.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
                if (checkbox.isChecked()) {
                    Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
                }
            }
        });
        
        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(HelloFormStuff.this, "ON", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(HelloFormStuff.this, "OFF", Toast.LENGTH_SHORT).show();
                }
            }
        });
        
        Spinner s = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.planets, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        
        final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
        final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
        radio_red.setOnClickListener(j_listener);
        radio_blue.setOnClickListener(j_listener);
        
    }
    
    OnClickListener j_listener = new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
        }
    };
}

5) Result Image

Reference http://www.androidside.com

댓글 없음:

댓글 쓰기