|
|
|
ทํา dropdownlist บน แอนดรอยด์ (android) ใช้ widgets ตัวไหนครับ |
|
|
|
|
|
|
|
ใช้ Spinner ครับ
activity_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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="26dp" />
</RelativeLayout>
Code
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
List<String> arrList = new ArrayList<String>();
TextView txtView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// spinner1
Spinner spin = (Spinner) findViewById(R.id.spinner1);
arrList.add("Mercury");
arrList.add("Venus");
arrList.add("Earth");
arrList.add("Mars");
ArrayAdapter<String> arrAd = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,
arrList);
spin.setAdapter(arrAd);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int i, long l) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
String.valueOf("Your Selected : " + arrList.get(i)),
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
String.valueOf("Your Selected Empty"),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Go to : Spinner - Android Widgets Example
|
|
|
|
|
Date :
2012-07-05 21:50:11 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|