【问题标题】:How to make a spinner inside a fragment?如何在片段内制作微调器?
【发布时间】:2021-10-01 13:48:14
【问题描述】:

我正在开发一个需要多个选项卡的小应用程序,每个选项卡都有不同的数据输入,使用片段一切正常,其中一个输入必须以微调器的形式进行,但这样做会导致“无法解决'父'和'位置'上的符号“,'android.widget.ArrayAdapter'中的'createFromResource(android.content.Context,int,int)'不能应用于'(com.example.sinalt2.fragment2,int, int)',代码在这里,任何帮助表示赞赏。


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment2, container, false);

        Spinner spinner = findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.documento, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String text = parent.getItemPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".fragment2">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="43dp"
        android:layout_marginEnd="8dp"
        android:text="Nome *" />

    <EditText
        android:id="@+id/editTextTextPersonName4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="180dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="Digite seu nome"
        android:inputType="textPersonName"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="92dp"
        android:layout_marginEnd="8dp"
        android:text="Tipo de documento *" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="260dp"
        android:layout_marginTop="78dp"
        android:layout_marginEnd="8dp"
        android:entries="@array/documento"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="66dp"
        android:layout_marginTop="140dp"
        android:layout_marginEnd="8dp"
        android:text="Nº Documento *" />

    <EditText
        android:id="@+id/editTextNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="180dp"
        android:layout_marginTop="125dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:inputType="number"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="102dp"
        android:layout_marginTop="190dp"
        android:layout_marginEnd="8dp"
        android:text="Telefone *" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="180dp"
        android:layout_marginTop="176dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:inputType="phone"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="114dp"
        android:layout_marginTop="245dp"
        android:layout_marginEnd="8dp"
        android:text="E-mail *" />

    <EditText
        android:id="@+id/editTextTextEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="180dp"
        android:layout_marginTop="226dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="70dp"
        android:layout_marginTop="290dp"
        android:layout_marginEnd="8dp"
        android:text="Município/UF *" />

</FrameLayout>

【问题讨论】:

    标签: java android android-fragments spinner


    【解决方案1】:

    onCreateView里面的代码需要修改如下,onCreateView的返回类型是View。我还假设微调器位于 fragment_fragment2 布局内。

    对于ArrayAdapter.createFromResource,您还需要传递上下文。由于您在片段中,this 指的是Fragment。因此你需要通过getAcitvity()

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment2, container, false);
    
        Spinner spinner = view.findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter =  ArrayAdapter.createFromResource(getActivity(),R.array.documento, 
        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
        return view;
    }
    

    更新: 你还需要修改onItemSelected里面的代码

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
        String text = parent.getItemAtPosition(i).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
    }
    

    【讨论】:

    • 很遗憾错误没有修复,仍然无法解决“父”和“位置”的符号,并保留“createFromResource(android.content.Context, int, int)' 'android.widget.ArrayAdapter' 不能应用于第 7 行的 '(com.example.sinalt2.fragment2, int, int)"。
    • @LucasdaCostaSilva 你能添加你的布局文件fragment_fragment2吗?
    • @LucasdaCostaSilva 我已经更新了答案。请检查它是否有效。
    • 添加了完整的fragment_fragment2代码,解决了更大的问题,但是'父'和'位置'上的“无法解决符号”仍然存在。
    • @LucasdaCostaSilva 我已更新解决方案以添加 onItemSelected。让我知道它是否有效。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多