我们可以在Android

时间:2018-04-03 09:42:38

标签: android android-studio build.gradle

我想从android中的微调器中获取多个Input。所以在接下来的这篇文章中,我想让Spinner像复选框一样获取多个输入。 有人能告诉我解决这个问题的方法吗?

我有另一个问题。

我遇到了“跑”的问题。

当我点击“运行”时,它只会将之前构建的Apk安装到设备上。

问题是它既没有运行Build.gradle也没有构建一个新的Apk,其中的更改可能反映在点击“run”。

意味着每次都必须单击Build Apk按钮,然后我必须单击“运行”然后将新的Apk安装到设备。

请帮助我,我正在开展一个重要的项目,我没有太多时间,我必须尽快交付。 请。

2 个答案:

答案 0 :(得分:0)

你不能用android提供的简单微调器做到这一点,你必须自定义微调器。有关更多信息,你可以参考这个link,在这个答案中,用户使用textview和复选框输入多个选项。

答案 1 :(得分:0)

使用自定义,您可以实现目标(具有精确的微调器效果和完整的解决方案,如下所示)

用于添加spinner等功能的xml文件:

           <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:weightSum="1">

                    <LinearLayout
                        android:id="@+id/linear_combination_of_country"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/tv_com_of_country"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:layout_marginStart="3dp"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:drawablePadding="15dp"
                            android:paddingBottom="5dp"
                            android:paddingTop="10dp"
                            android:text="country"
                            android:textColor="@color/black"
                            android:textSize="16sp" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="25dp"
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:layout_weight="0.01"
                            android:paddingTop="5dp"
                            android:src="@drawable/down_arrow" />
                    </LinearLayout>

                    <com.example.Utils.NoDefaultSpinner
                        android:id="@+id/spinner_combination_of_country"
                        android:layout_width="0dp"
                        android:layout_height="0dp" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="0.5dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="1dp"
                        android:background="@color/black" />


                </LinearLayout>

在您的Activity类中添加以下内容:

linear_combination_of_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                spinner_combination_of_city.performClick();
            }
        });


linear_combination_of_city.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once
                linear_combination_of_city.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                spinner_combination_of_city.setDropDownWidth(linear_combination_of_city.getWidth() - 30);
                // Here you can get the size :)
            }
        });         

spinner_combination_of_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String item = parent.getItemAtPosition(position).toString();
                tv_com_of_city.setText("" + item);
            }

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

            }
        });

combinationOfCountryList();
FilterMultiSelectAdapter myCountryAdapter = new FilterMultiSelectAdapter(getContext(), 0,
                multiSelectCountryList);
        spinner_combination_of_country.setAdapter(myCountryAdapter);


private void combinationOfCountryList(){
    ArrayList<FamilyModel> multiSelectCountryList = new ArrayList<>();

    List<String> combinationOfCountry = new ArrayList<String>();
    combinationOfCountry.add("UK");
    combinationOfCountry.add("US");
    combinationOfCountry.add("Asia");

    for (int i = 0; i < combinationOfCountry.size(); i++) {
        FamilyModel model = new FamilyModel();
        model.setTitle(combinationOfCountry.get(i));
        model.setSelected(false);
        multiSelectCountryList.add(model);
    }
}   

您的模型类

public class FamilyModel {
private String title;
private boolean selected;
private boolean isChild = false;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

public boolean isChild() {
    return isChild;
}

public void setChild(boolean child) {
    isChild = child;
}

}

适配器类:

public class FilterMultiSelectAdapter extends ArrayAdapter<FamilyModel> {

private Context mContext;
private ArrayList<FamilyModel> familyList;


public FilterMultiSelectAdapter(Context context,  int resource, List<FamilyModel> objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.familyList = (ArrayList<FamilyModel>) objects;
}

@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(final int position, View convertView,
                          ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater layoutInflator = LayoutInflater.from(mContext);
        convertView = layoutInflator.inflate(R.layout.design_filter_multi_select_view, null);
        holder = new ViewHolder();
        holder.mTextView = (TextView) convertView
                .findViewById(R.id.family_member_name);
        holder.toggle = (ToggleButton) convertView
                .findViewById(R.id.toggle);

        holder.familyLayout = (LinearLayout) convertView.findViewById(R.id.linear_checkbolx);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.mTextView.setText(familyList.get(position).getTitle());

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    if(familyList.get(position).isChild()){
        layoutParams.setMargins(120, 0, 0, 0);
        holder.familyLayout.setLayoutParams(layoutParams);

    }else{
        layoutParams.setMargins(0, 0, 0, 0);
        holder.familyLayout.setLayoutParams(layoutParams);
    }

    holder.mTextView.setTextColor(ContextCompat.getColor(mContext,R.color.black));

    holder.toggle.setTag(position);
    holder.toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int getPosition = (Integer) buttonView.getTag();
            if (buttonView.isChecked()) {
                familyList.get(getPosition).setSelected(true);
            } else {
                familyList.get(getPosition).setSelected(false);
            }
        }
    });


    holder.familyLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.toggle.isChecked())
                holder.toggle.setChecked(false);
            else
                holder.toggle.setChecked(true);
        }
    });


    if (familyList.get(position).isSelected()) {
        holder.toggle.setChecked(true);
    } else {
        holder.toggle.setChecked(false);
    }

    return convertView;
}

private class ViewHolder {
    private TextView mTextView;
    private ToggleButton toggle;
    private LinearLayout familyLayout;
}

}

design_filter_multi_select_view.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <LinearLayout
        android:id="@+id/linear_checkbolx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="@drawable/action_bar_hover"
        android:padding="8dp">

        <ToggleButton
            android:id="@+id/toggle"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/filter_toggle_status"
            android:checked="false"
            android:text=""
            android:textOff=""
            android:textOn="" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/family_member_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:gravity="center_vertical"
                    android:text="Hello"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:textColor="@color/reminder_text_color"
                    android:textSize="16sp" />


            </LinearLayout>

        </LinearLayout>


    </LinearLayout>

</RelativeLayout> 

自定义Spinner类:

公共类NoDefaultSpinner扩展Spinner {

public NoDefaultSpinner(Context context) {
    super(context);
}

public NoDefaultSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public NoDefaultSpinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void setAdapter(SpinnerAdapter orig ) {
    final SpinnerAdapter adapter = newProxy(orig);

    super.setAdapter(adapter);

    try {
        final Method m = AdapterView.class.getDeclaredMethod(
                "setNextSelectedPositionInt",int.class);
        m.setAccessible(true);
        m.invoke(this,-1);

        final Method n = AdapterView.class.getDeclaredMethod(
                "setSelectedPositionInt",int.class);
        n.setAccessible(true);
        n.invoke(this,-1);
    }
    catch( Exception e ) {
        throw new RuntimeException(e);
    }
}

protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
    return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
            obj.getClass().getClassLoader(),
            new Class[]{SpinnerAdapter.class},
            new SpinnerAdapterProxy(obj));
}



/**
 * Intercepts getView() to display the prompt if position < 0
 */
protected class SpinnerAdapterProxy implements InvocationHandler {

    protected SpinnerAdapter obj;
    protected Method getView;


    protected SpinnerAdapterProxy(SpinnerAdapter obj) {
        this.obj = obj;
        try {
            this.getView = SpinnerAdapter.class.getMethod(
                    "getView",int.class,View.class,ViewGroup.class);
        }
        catch( Exception e ) {
            throw new RuntimeException(e);
        }
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
        try {
            return m.equals(getView) &&
                    (Integer)(args[0])<0 ?
                    getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) :
                    m.invoke(obj, args);
        }
        catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected View getView(int position, View convertView, ViewGroup parent)
            throws IllegalAccessException {

        if( position<0 ) {
            final TextView v =
                    (TextView) ((LayoutInflater)getContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                            android.R.layout.simple_spinner_item,parent,false);
            v.setText(getPrompt());
            return v;
        }
        return obj.getView(position,convertView,parent);
    }
}

}

相关问题