我想创建具有多个选择的微调器,例如用户可以选择多个项目并在edittext中获取该项目,因为我正在关注此示例但它无法正常工作 我的片段类中的http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html
在这里查看此链接我得到了活动类的答案,但不是片段 Get the alert dialog selected value to edittext
答案 0 :(得分:1)
如果您正在阅读本教程" http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html"它在Activity中正常工作。
如果您在为Fragment创建相同内容时遇到问题,请输入以下代码。
<强> main_layout.xml 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</FrameLayout>
</RelativeLayout>
<强> fragment_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.testandroid.MultiSelectionSpinner
android:id="@+id/mySpinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mySpinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:onClick="onClick"
android:text="Get Items" />
<EditText
android:id="@+id/values"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MyFragment.java类
public class MyFragment extends Fragment {
MultiSelectionSpinner spinner;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate (R.layout.fragment_layout, null);
String[] array = { "one", "two", "three" };
spinner = (MultiSelectionSpinner) v.findViewById (R.id.mySpinner1);
spinner.setItems (array);
final EditText values = (EditText) v.findViewById (R.id.values);
Button button1 = (Button) v.findViewById (R.id.button1);
button1.setOnClickListener (new OnClickListener() {
@Override
public void onClick (View v) {
String s = spinner.getSelectedItemsAsString();
values.setText (s);
}
});
return v;
}
}
在您的主要活动中
MyFragment fragment = new MyFragment ();
FragmentTransaction ft = getFragmentManager ().beginTransaction ();
ft.replace (R.id.container, fragment, "fragment");
ft.commit ();
屏幕1
屏幕2