pertama membuat actifitymain "a.xml"
<?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" >
<Spinner
android:id="@+id/spiner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih" />
<TextView
android:id="@+id/o"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
----------------------------------------------------------------------------------------------------------------------------- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spiner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih" />
<TextView
android:id="@+id/o"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
tampilan
mainactifyty "MainActivity.java"
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
TextView t;
String[] items = { "JAVA", "PHP", "ANDROID" };
Button k;
Spinner s;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.a);
t = (TextView) findViewById(R.id.o);
k = (Button) findViewById(R.id.b);
s = (Spinner) findViewById(R.id.spiner);
k.setOnClickListener(new ok());
s.setOnItemSelectedListener(this);
ArrayAdapter<String> araybaru = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
araybaru.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(araybaru);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
t.setText("");
}
class ok implements Button.OnClickListener {
public void onClick(View v) {
String a = (String) ("Pilihan anda adalah : "+s.getSelectedItem());
t.setText(a);
}
}
}
raning hasilnya.
lalu saya memilih java
lalu saya click tombol pilih...