Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
GUI Widget Lecture6 Bu1on • A clickable widget with a text label • Key a1ributes: android:clickable=“true” android:id=“@+id/uniqueID” android:onClick=“funcJonName” android:text=“bu1onText” Event Handling -‐-‐ 1 /* *funcJon to call in acJvity when clicked *must be public, void, and take a View argument */ public void funcJonName(View view) [ } Event Handling -‐-‐ 2 In Java file: Bu1on firstBu1on = (Bu1on) findViewById(R.id.uniqueID); firstBu1on.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { …. } }); RadioBu1on • A toggleable on/off switch; part of a group • Key a1ributes: Android:checked=“true” set to true to make it iniJally checked android:clickable=“true” android:id=“@+id/uniqueID” android:onClick=“funcJonName” android:text=“bu1onText” RadioBu1on • Need to be nested inside a RadioGroup tag in XML so that only one can be selected at a Jme <RadioGroup android:orienta4on="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioBu=on android:id="@+id/leo" android:onClick="pickTurtle" android:text="Leo" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioBu=on android:id="@+id/mike" android:onClick="pickTurtle" android:text="Mike" android:layout_width="wrap_content" android:layout_height="wrap_content" /> … </RadioGroup> Reusing onClick handler MainAcJvity.java public class MainAcJvity extends AcJvity { // check which radio bu/on was clicked public void pickTurtle(View view) { if (view.getId() == R.id.leo) { … } else if (view.getId() == R.id.mike) { … } else if (view.getId() == R.id.don) { … } else if (view.getId() == R.id.raph) { … } } Spinner • A drop-‐down menu of selectable choices • Key a1ributes: android:clickable=“true” if false, disable the spinner android:id=“@+id/uniqueID” android:entries=“@array/arrayName” opJons android:prompt=“@string/text” Jtle text String resources • Declare constant strings and arrays in res/values/ strings.xml <?xml version="1.0" encoding="uM-‐8"?> <resources> <string name=“bu1onText”> CLICK </string> <string-‐array name=”arrayName"> <item>value1</item> <item>value2</item> <item>value3</item> </string-‐array> </resources> Handler • Refer to them in Java code: – As a resource: R.string.name, R.array.name – As a string or array: getResources().getString(R.string.name) getResources().getStringArray(R.array.name) • Handle events: – Must get the Spinner object using findViewById – Then call its setOnItemSelectedListener method acJvity_main.xml <RelaJveLayout> <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/unit_type_spinner" android:entries=“@array/conversionTypes” android:prompt=“@string/choose_type” android:layout_below="@+id/amount_text_view" android:layout_alignParentLeV="true" android:layout_alignParentStart="true"/> </RealJveLayout> strings.xml In res/values/strings.xml <?xml version="1.0" encoding="uM-‐8"?> <resources> <string name=“choose_type”> Choose Conversion Type </ string> <string-‐array name=”conversionTypes"> <item>Gallon</item> <item>Liter</item> <item>Cup</item> </string-‐array> </resources> MainAcJvity.java public void addItemsToUnitTypeSpinner(){ // Get a reference to the spinner unitTypeSpinner = (Spinner) findViewById(R.id.unit_type_spinner); // Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter<CharSequence> unitTypeSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.conversionTypes, android.R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears unitTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner _dropdown_item); // Apply the adapter to the spinner unitTypeSpinner.setAdapter(unitTypeSpinnerAdapter); } MainAcJvity.java public void addListenerToUnitTypeSpinner() { unitTypeSpinner = (Spinner) findViewById(R.id.unit_type_spinner); unitTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3) { // Get the item selected in the Spinner String itemSelectedInSpinner = parent.getItemAtPosiJon(pos).toString(); … } public void onNothingSelected(AdapterView<?> arg0) { // TODO maybe add something here later } }); }