【Android源码 栏目提醒】:网学会员为需要Android源码 的朋友们搜集整理了SpinnerActivity java android源码 - 开发文档相关资料,希望对各位网友有所帮助!
/ Copyright C 2010 The Android Open Source Project Licensed under the Apache License Version 2.0 the License you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing software distributed under the License is distributed on an AS IS BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. See the License for the specific language governing permissions and limitations under the License. / package com.android.example.spinner import com.android.example.spinner.R import android.app.Activity import android.content.Context import android.content.SharedPreferences import android.os.Bundle import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Spinner import android.widget.TextView import android.widget.Toast import android.widget.AdapterView.OnItemSelectedListener / Displays an Android spinner widget backed by data in an array. The array is loaded from the strings.xml resources file. / public class SpinnerActivity extends Activity / Fields to contain the current position and display contents of the spinner / protected int mPos protected String mSelection / ArrayAdapter connects the spinner widget to array-based data. / protected ArrayAdapter mAdapter / The initial position of the spinner when it is first installed. / public static final int DEFAULT_POSITION 2 / The name of a properties file that stores the position and selection when the activity is not loaded. / public static final String PREFERENCES_FILE SpinnerPrefs / These values are used to read and write the properties file. PROPERTY_DELIMITER delimits the key and value in a Java properties file. The marker strings are used to write the properties into the file / public static final String PROPERTY_DELIMITER / The key or label for position in the preferences file / public static final String POSITION_KEY Position / The key or label for selection in the preferences file / public static final String SELECTION_KEY Selection public static final String POSITION_MARKER POSITION_KEY PROPERTY_DELIMITER public static final String SELECTION_MARKER SELECTION_KEY PROPERTY_DELIMITER / Initializes the application and the activity. 1 Sets the view 2 Reads the spinners backing data from the string resources file 3 Instantiates a callback listener for handling selection from the spinner Notice that this method includes code that can be uncommented to force tests to fail. This method overrides the default onCreate method for an Activity. see android.app.ActivityonCreateandroid.os.Bundle / Override public void onCreateBundle savedInstanceState / derived classes that use onCreate overrides must always call the super constructor / super.onCreatesavedInstanceState setContentViewR.layout.main Spinner spinner Spinner findViewByIdR.id.Spinner01 / Create a backing mLocalAdapter for the Spinner from a list of the planets. The list is defined by XML in the strings.xml file. / this.mAdapter ArrayAdapter.createFromResourcethis R.array.Planets android.R.layout.simple_spinner_dropdown_item / Attach the mLocalAdapter to the spinner. / spinner.setAdapterthis.mAdapter / Create a listener that is triggered when Android detects the user has selected an item in the Spinner. / OnItemSelectedListener spinnerListener new myOnItemSelectedListenerthisthis.mAdapter / Attach the listener to the Spinner. / spinner.setOnItemSelectedListenerspinnerListener / To demonstrate a failure in the preConditions test uncomment the following line. The test will fail because the selection listener for the Spinner is not set. / // spinner.setOnItemSelectedListenernull / A callback listener that implements the link android.widget.AdapterView.OnItemSelectedListener interface For views based on adapters this interface defines the methods available when the user selects an item from the View. / public class myOnItemSelectedListener implements OnItemSelectedListener / provide local instances of the mLocalAdapter and the mLocalContext / ArrayAdapter mLocalAdapter Activity mLocalContext / Constructor param c - The activity that displays the Spinner. param ad - The Adapter view that controls the Spinner. Instantiate a new listener object. / public myOnItemSelectedListenerActivity c ArrayAdapter ad this.mLocalContext c this.mLocalAdapter ad / When the user selects an item in the spinner this method is invoked by the callback chain. Android calls the item selected listener for the spinner which invokes the onItemSelected method. see android.widget.AdapterView.OnItemSelectedListeneronItemSelected android.widget.AdapterView android.view.View int long param parent - the AdapterView for this listener param v - the View for this listener param pos - the 0-based position of the selection in the mLocalAdapter param row - the 0-based row number of the selection in the View / public void onItemSelectedAdapterView parent View v int pos long row SpinnerActivity.this.mPos pos SpinnerActivity.this.mSelection parent.getItemAtPositionpos.toString / Set the value of the text field in the UI / TextView resultText TextViewfindViewByIdR.id.SpinnerResult resultText.setTextSpinnerActivity.this.mSelection / The definition of OnItemSelectedListener requires an override of onNothingSelected even though this implementation does not use it. param parent - The View for this Listener / public void onNothingSelectedAdapterView parent // do nothing / Restores the current state of the spinner which item is selected and the value of that item. Since o上一篇:Android手机拍照功能源码