Choose mode wizard fragment

This commit is contained in:
serso 2015-01-29 20:17:07 +01:00
parent 09f2f1307d
commit e0eabca868
3 changed files with 76 additions and 71 deletions

View File

@ -22,15 +22,18 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.RadioButton; import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.wizard.CalculatorMode.*; import static org.solovyev.android.calculator.wizard.CalculatorMode.*;
@ -39,7 +42,7 @@ import static org.solovyev.android.calculator.wizard.CalculatorMode.*;
* Date: 6/16/13 * Date: 6/16/13
* Time: 9:59 PM * Time: 9:59 PM
*/ */
public class ChooseModeWizardStep extends WizardFragment { public class ChooseModeWizardStep extends WizardFragment implements AdapterView.OnItemSelectedListener {
/* /*
********************************************************************** **********************************************************************
@ -59,11 +62,8 @@ public class ChooseModeWizardStep extends WizardFragment {
********************************************************************** **********************************************************************
*/ */
@Nullable private Spinner spinner;
private RadioButton simpleModeRadioButton; private TextView description;
@Nullable
private RadioButton engineerModeRadioButton;
private CalculatorMode mode; private CalculatorMode mode;
@ -97,34 +97,25 @@ public class ChooseModeWizardStep extends WizardFragment {
public void onViewCreated(View root, Bundle savedInstanceState) { public void onViewCreated(View root, Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState); super.onViewCreated(root, savedInstanceState);
simpleModeRadioButton = (RadioButton) root.findViewById(R.id.wizard_simple_mode_radiobutton); spinner = (Spinner) root.findViewById(R.id.wizard_mode_spinner);
engineerModeRadioButton = (RadioButton) root.findViewById(R.id.wizard_engineer_mode_radiobutton); spinner.setAdapter(new MyArrayAdapter(getActivity()));
spinner.setOnItemSelectedListener(this);
switch (mode) { description = (TextView) root.findViewById(R.id.wizard_mode_description);
case simple: updateDescription();
simpleModeRadioButton.setChecked(true); }
engineerModeRadioButton.setChecked(false);
break; private void updateDescription() {
case engineer: description.setText(mode == simple ? R.string.cpp_wizard_mode_simple_description : R.string.cpp_wizard_mode_engineer_description);
simpleModeRadioButton.setChecked(false);
engineerModeRadioButton.setChecked(true);
break;
}
} }
@Nonnull @Nonnull
CalculatorMode getSelectedMode() { CalculatorMode getSelectedMode() {
CalculatorMode mode = getDefaultMode(); if (spinner != null) {
return mode;
if (simpleModeRadioButton != null && simpleModeRadioButton.isChecked()) {
mode = simple;
} }
if (engineerModeRadioButton != null && engineerModeRadioButton.isChecked()) { return getDefaultMode();
mode = engineer;
}
return mode;
} }
@Override @Override
@ -133,4 +124,32 @@ public class ChooseModeWizardStep extends WizardFragment {
outState.putSerializable(MODE, mode); outState.putSerializable(MODE, mode);
} }
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mode = position == 0 ? simple : engineer;
updateDescription();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
private static final class MyArrayAdapter extends ArrayAdapter<String> {
public MyArrayAdapter(Context context) {
super(context, android.R.layout.simple_spinner_item, context.getResources().getStringArray(R.array.cpp_modes));
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
if (view instanceof TextView) {
((TextView) view).setTextAppearance(getContext(), android.R.style.TextAppearance_Large);
}
return view;
}
}
} }

View File

@ -23,50 +23,31 @@
--> -->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="wrap_content" a:layout_width="match_parent"
a:layout_height="match_parent" a:layout_height="match_parent"
a:orientation="vertical"> a:orientation="vertical"
a:gravity="center">
<RadioGroup <TextView
a:orientation="horizontal" a:layout_width="wrap_content"
a:layout_width="match_parent" a:layout_height="wrap_content"
a:layout_height="wrap_content" a:textAppearance="@android:style/TextAppearance.Large"
a:gravity="center_vertical|top"> style="@style/WizardLabel"
a:text="Choose mode"
a:layout_marginBottom="40dp" />
<RadioButton a:layout_width="0dp" <Spinner
a:layout_weight="1" a:id="@+id/wizard_mode_spinner"
a:layout_margin="6dp" a:layout_width="wrap_content"
a:id="@+id/wizard_simple_mode_radiobutton" a:layout_height="wrap_content"
a:text="@string/cpp_wizard_mode_simple" style="@style/WizardLabel"
a:layout_height="wrap_content"/> a:layout_marginBottom="40dp" />
<RadioButton a:layout_width="0dp" <TextView
a:layout_weight="1" a:id="@+id/wizard_mode_description"
a:layout_margin="6dp" a:layout_width="wrap_content"
a:id="@+id/wizard_engineer_mode_radiobutton" a:layout_height="wrap_content"
a:text="@string/cpp_wizard_mode_engineer" style="@style/WizardLabel"
a:layout_height="wrap_content"/> a:text="Choose mode" />
</RadioGroup>
<LinearLayout a:layout_width="match_parent"
a:layout_height="wrap_content"
a:orientation="horizontal">
<TextView
a:layout_width="0dp"
a:layout_weight="1"
a:layout_height="wrap_content"
a:padding="6dp"
a:text="@string/cpp_wizard_mode_simple_description"/>
<TextView
a:layout_width="0dp"
a:layout_weight="1"
a:layout_height="wrap_content"
a:padding="6dp"
a:text="@string/cpp_wizard_mode_engineer_description"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -133,4 +133,9 @@
<item>green</item> <item>green</item>
<item>grey</item> <item>grey</item>
</string-array> </string-array>
<string-array name="cpp_modes">
<item>@string/cpp_wizard_mode_simple</item>
<item>@string/cpp_wizard_mode_engineer</item>
</string-array>
</resources> </resources>