Android-spinner-control
提供:Dev Guides
Android-スピナー
'_Spinnerでは、ドロップダウンメニューから項目を選択できます_
例えば。 Gmailアプリケーションを使用している場合、下に示すようにドロップダウンメニューが表示されます。ドロップダウンメニューから項目を選択する必要があります。
スピナーの例
例
この例では、コンピューターのカテゴリーを示しています。カテゴリーからカテゴリーを選択する必要があります。
この例を試すには、以下の手順に従ってアプリケーションを開発した後、実際のデバイスでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application and name it as AndroidSpinnerExample under a package com.example.spinner. |
2 | Modify src/AndroidSpinnerExampleActivity.java file to create a simple list view with items which are showing as spinner items |
3 | Modify res/layout/activity_main.xml file to add respective XML code. |
4 | No need to define default string constants. Android studio takes care of default string constants at string.xml |
5 | Run the application and choose a running android device and install the application on it and verify the results. |
以下は、変更されたメインアクティビティファイル* src/com.example.spinner/AndroidSpinnerExampleActivity.java。*のコンテンツです。
package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Spinner click listener
spinner.setOnItemSelectedListener(this);
//Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
//Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
//Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
//TODO Auto-generated method stub
}
}
*res/layout/activity_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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title"/>
</LinearLayout>
*res/values/string.xml* を次のように変更します
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidSpinnerExample</string>
</resources>
これはデフォルトの AndroidManifest.xml です
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.spinner" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.spinner.AndroidSpinnerExampleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
AndroidSpinnerExampleアプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
スピナーボタンをクリックすると、下に示すようにドロップダウンメニューが表示されます