//SpinnerActivity.java
package csie.stut.edu.tw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SpinnerActivity extends Activity {
/** Called when the activity is first created. */
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(button1_listener);
}
private Button.OnClickListener button1_listener = new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SpinnerActivity.this, Spinner2Activity.class);
startActivity(intent);
}
};
}
=================================================================
//Spinner2Activity.java
package csie.stut.edu.tw;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
public class Spinner2Activity extends Activity {
Spinner spinner_c;
EditText edit_text1;
private ArrayList<String> allcountries;
private ArrayAdapter<String> aspnCountries;
private static final String[] mCountries = { "China", "Russia", "Germany",
"Japan", "Korea", "USA" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Spinner2Activity");
setContentView(R.layout.spinner);
find_and_modify_view();
spinner_c.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// your code here
edit_text1 = (EditText) findViewById(R.id.editText1);
edit_text1.setText(parentView.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
private void find_and_modify_view() {
// TODO Auto-generated method stub
spinner_c = (Spinner) findViewById(R.id.spinner1);
allcountries = new ArrayList<String>();
for (int i = 0; i < mCountries.length; i++) {
allcountries.add(mCountries[i]);
}
aspnCountries = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, allcountries);
aspnCountries
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_c.setAdapter(aspnCountries);
}
}
=================================================================
//main.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啟動Spinner" >
</Button>
</LinearLayout>
========================================================================
//spinner.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" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spinner_1" >
<requestFocus >
</requestFocus>
</EditText>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</Spinner>
</LinearLayout>
========================================================================
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="csie.stut.edu.tw"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SpinnerActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Spinner2Activity" >
</activity>
</application>
</manifest>
沒有留言:
張貼留言
請提供您寶貴的意見