1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android Spinner –下拉列表

Android Spinner –下拉列表

时间:2019-05-23 04:46:36

相关推荐

Android Spinner –下拉列表

This tutorial will give you a hands on experience in using Android Spinner as a drop down menu, passing data using android bundle and showing popup notification using android toast.

本教程将为您提供使用Android Spinner作为下拉菜单,使用android bundle传递数据以及使用android toast显示弹出通知的实践经验。

We will create an android application that consists of a simple spinner that allows selecting an item from a drop down list. We will display static data in the spinner. Selecting an item from spinner would display atoast message.

我们将创建一个包含一个简单微调器的android应用程序,该微调器允许从下拉列表中选择一个项目。 我们将在微调器中显示静态数据。 从微调器中选择一个项目将显示一条祝酒消息

To pass data in the form of bundles between activities, we’ll use a button to perform an intent and display the data passed to the next screen.

为了在活动之间以束的形式传递数据,我们将使用按钮执行意图并显示传递到下一个屏幕的数据。

Android微调器 (Android Spinner)

Android Spinner is just a drop down list similar to what’s seen in other programming languages such as in HTML pages.

Android Spinner只是一个下拉列表,类似于在其他编程语言(例如HTML页面)中看到的内容。

In Android, Spinner is used to select one value from a set of values. In the default state, a spinner shows its currently selected value. Touching the spinner displays a drop down menu with all other available values, from which the user can select a new one.

在Android中,Spinner用于从一组值中选择一个值。 在默认状态下,微调器显示其当前选定的值。 触摸微调器将显示一个具有所有其他可用值的下拉菜单,用户可以从中选择一个新的值。

Android spinner is associated withAdapterView. So we need to set the adapter class with the Spinner.

Android微调器与AdapterView关联。 因此,我们需要使用Spinner设置适配器类。

Android下拉列表 (Android Drop Down List)

Following xml file shows the layout of a typical spinner in android which consists of a text label and a spinner element tag.

以下xml文件显示了android中典型的微调框的布局,该布局由文本标签和微调框元素标签组成。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:padding="10dip"android:layout_width="fill_parent"android:layout_height="wrap_content"><!-- Text Label --><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dip"android:text="Category:"android:layout_marginBottom="5dp"/><!-- Spinner Element --><Spinner android:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"android:prompt="@string/spinner_title"/></LinearLayout>

Following snippet shows how to use a spinner in the activity class.

以下片段显示了如何在活动类中使用微调器。

Spinner spinner = (Spinner) findViewById(R.id.spinner);

Let’s develop an application where we pass the selected value from the Spinner onto the next screen using Bundles and display a Toast message of the selected value at the same time.

让我们开发一个应用程序,在该应用程序中,我们使用Bundles将微调器中的选定值传递到下一个屏幕,并同时显示选定值的Toast消息。

Android Spinner示例项目结构 (Android Spinner Example Project Structure)

Below image shows the android studio project for spinner example.

下图显示了微调器示例的android studio项目。

Let’s start with the layout of the MainActivity class. We just need to add Button to thebasic_spinner.xmlfile.

让我们从MainActivity类的布局开始。 我们只需要将Button添加到basic_spinner.xml文件中。

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><LinearLayoutandroid:orientation="vertical"android:padding="10dip"android:id="@+id/linear_layout"android:layout_width="fill_parent"android:layout_height="wrap_content"><!-- Text Label --><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dip"android:text="Category:"android:layout_marginBottom="5dp"/><!-- Spinner Element --><Spinnerandroid:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"android:prompt="@string/spinner_title"/></LinearLayout><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="NEXT"android:id="@+id/button"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="137dp" /></RelativeLayout>

The layout of theSecondActivityis as follows:

SecondActivity的布局如下:

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium"android:text="Empty"android:id="@+id/txt_bundle"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="103dp" /></RelativeLayout>

Here is the Android Manifest file.

这是Android Manifest文件。

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package=".spinners" ><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".SecondActivity"/></application></manifest>

TheMainActivityandSecondActivityjava classes are defined as follows.

MainActivitySecondActivityJava类的定义如下。

package .spinners;import android.app.Activity;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Spinner elementfinal Spinner spinner = (Spinner) findViewById(R.id.spinner);Button button=(Button)findViewById(R.id.button);// Spinner click listenerspinner.setOnItemSelectedListener(this);// Spinner Drop down elementsList<String> categories = new ArrayList<String>();categories.add("Item 1");categories.add("Item 2");categories.add("Item 3");categories.add("Item 4");categories.add("Item 5");categories.add("Item 6");// Creating adapter for spinnerArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);// Drop down layout style - list view with radio buttondataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// attaching data adapter to spinnerspinner.setAdapter(dataAdapter);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent= new Intent(MainActivity.this,SecondActivity.class);intent.putExtra("data",String.valueOf(spinner.getSelectedItem()));startActivity(intent);}});}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {// On selecting a spinner itemString item = parent.getItemAtPosition(position).toString();// Showing selected spinner itemToast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();}public void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}}

package .spinners;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class SecondActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second_activity);TextView textView=(TextView) findViewById(R.id.txt_bundle);Bundle bundle=getIntent().getExtras();String data=bundle.get("data").toString();textView.setText(data);}}

In the above code, we’ve displayed a toast when an item from the spinner dropdown menu is selected. On button click we pass the selected spinner item as a string value to the next activity using android bundle. Then the data is retrieved from the bundle and displayed in a TextView. Quick easy and simple, isn’t it?

在上面的代码中,当从微调器下拉菜单中选择一个项目时,我们已经显示了一个祝酒词。 在单击按钮时,我们使用android bundle将选定的微调项作为字符串值传递给下一个活动。 然后,从包中检索数据并显示在TextView中。 快速简便,不是吗?

The screenshots of the app are shown below. I am running it on one of the emulators.

该应用程序的屏幕截图如下所示。 我正在其中一个仿真器上运行它。

The first screen shows the drop down list contents when the Spinner is opened.

打开微调框时,第一个屏幕显示下拉列表的内容。

After an item is selected, Toast notification message appears for some time.

选择一个项目后,Toast通知消息出现一段时间。

After sometime toast notification disappears as shown in below image. It doesn’t stop us in clicking the next button.

一段时间后,烤面包通知消失,如下图所示。 这并不能阻止我们单击下一步。

Finally, in the second screen, the selected item from the dropdown list is retrieved using Bundles and displayed in the TextView.

最后,在第二个屏幕中,使用Bundles检索下拉列表中的选定项目并显示在TextView中。

Below is a sample run of our android spinner example application in emulator.

以下是在模拟器中运行我们的Android Spinner示例应用程序的示例运行。

That’s all for now, we will look into Android ListView in next post. You can downloadAndroid Spinner, Bundle and Toastexample project from below link.

到此为止,我们将在下一篇文章中研究Android ListView 。 您可以从下面的链接下载Android Spinner,Bundle和Toast示例项目。

Download Android Spinner, Bundle and Toast Example Project下载Android Spinner,Bundle和Toast示例项目

Reference: Official Doc

参考: 官方文件

翻译自: /9231/android-spinner-drop-down-list

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。