代码无法在spinner android

时间:2016-10-29 21:51:57

标签: android material-design android-spinner

国家和城市的下拉列表:

Dropdown list for country and city

我想在android中创建一个微调器就像这个附加图像一样。例如,您选择列表,您可以从国家/地区选择任何城市。只有城市应该是可点击的,并且可以从列表中存储。国家/地区名称不应该是可点击的。在从微调器中禁用某些项目时遇到问题。 这是微调器的xml。

 `<Spinner
        android:id="@+id/districtSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="District"
        android:textSize="16sp"
        android:theme="@style/MyEditText"

      />`

这是代码:

`Spinner DistrictBetterSpinner;
 String[] District_Spinner = new String[]{"Punjab", "Rawalpindi", "Attock",      "Faisalabad", "Chakwal", "Bhakkar", "Jhelum", "Multan", "Chiniot"
        , "Bhawalpur", "Jhelum", "Gujrat", "Gujranwala", "Lahore", "Mianwali", "Khyber Pakhtunkhwa (KPK)", "Abbotabad", "Haripur", "Kohat",
        "Sindh", "Hyderabad", "Jacobabad", "Balochistan", "Awaran", "Gawadar", "Federally Administered Tribal Areas", "Khyber Agency",
        "Mohmat Agency", "Azad Jammu and Kashmir", "Neelum", "Bagh", "Gilgit Baltistan", "Skardu", "Astore"};
  List<String> spinnerlist;
  ArrayAdapter<String> arrayadapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_add_post); 
  DistrictBetterSpinner = (Spinner) findViewById(R.id.districtSpinner);
  spinnerlist = new ArrayList<>(Arrays.asList(District_Spinner));
  arrayadapter = new ArrayAdapter<String>(AddPostActivity.this, R.layout.spinner_item, spinnerlist);
  public boolean isEnabled(int position){
  if (position == 0 || position==1) {
  return false;
  } else {
  return true;
  }
  }

@Override
    public View getDropDownView ( int position, View convertView, ViewGroup parent){
        View view = super.getDropDownView(position, convertView, parent);
        TextView spinnerTextView = (TextView) view;
        if (position == 0) {
            // Set the disable item text color
            spinnerTextView.setTextColor(Color.BLACK);
        } else {
            spinnerTextView.setTextColor(Color.GRAY);
        }
        return view;
    }
}

;

 arrayadapter.setDropDownViewResource(R.layout.spinner_item);
DistrictBetterSpinner.setAdapter( arrayadapter);

`

1 个答案:

答案 0 :(得分:0)

我认为以下内容可能就是您所追求的内容(在将城市getDropDownView设置为灰色后添加到TextColor的新行): -

    @Override
    public View getDropDownView ( int position, View convertView, ViewGroup parent){
        View view = super.getDropDownView(position, convertView, parent);
        TextView spinnerTextView = (TextView) view;
        if (position == 0) {
            // Set the disable item text color
            spinnerTextView.setTextColor(Color.BLACK);
        } else {
            spinnerTextView.setTextColor(Color.GRAY);
            spinnerTextView.setOnclickListener( new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //YOUR ONCLICK HANDLING CODE HERE
                }
            });
        }
        return view;
    }

P.S。在上面的评论中,我提到getView作为替代。这不是选择视图而不是下拉列表(与ListViews混淆)。

仔细查看您的答案和代码。我看到你真的没有。所以这是一个基于您的代码的基本示例。

首先是下拉列表中一个条目的新布局文件(我也有一点作弊,并将其用于所选条目,每个可能有一个单独的布局)。这是 spinner_item.xml : -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/spinnerTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

如果你想要自定义功能,你应该有一个自定义适配器(这似乎是你的代码中的attemtping)。适配器处理数据(在您的情况下为arrayList)放入微调器使用的视图(使用的术语是膨胀)。所以这是我放入自己文件的自定义适配器。这是 MyArrayAdapter.java : -

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mike092015 on 31/10/2016.
 */
public class MyArrayAdapter extends ArrayAdapter<String> {

    private Context context;
    private TextView district;
    private ArrayList<String> districts;

    public MyArrayAdapter(Activity context, int layout, ArrayList<String> districts) {
        super(context, layout, districts);
        this.context = context;
        this.districts = districts;
    }

    @Override
    public View getDropDownView(int position, View convertview, ViewGroup parent) {

        View v = convertview;
        if (v == null) {
            v = LayoutInflater.from(this.context).inflate(R.layout.spinner_item,parent,false);
        }
        district = (TextView) v.findViewById(R.id.spinnerTextView);
        district.setText(districts.get(position));
        district.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"You clicked me",Toast.LENGTH_SHORT).show();
            }
        });
        return v;
    }

    public View getView(int position, View convertview, ViewGroup parent) {
        View v = convertview;
        if (v == null) {
            v = LayoutInflater.from(this.context).inflate(R.layout.spinner_item,parent,false);
        }
        district = (TextView) v.findViewById(R.id.spinnerTextView);
        district.setText(districts.get(position));
        return v;
    }
}

请注意onClickListener中的getDropDownView这是您捕获的位置并处理条目的点击(在这种情况下,只是发出祝酒词)。

activity_main.xml (包含微调器的布局,如果与布局有任何不同,则为: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Testing"/>
    <Spinner
        android:id="@+id/districtSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="District"
        android:textSize="16sp"
        />
</LinearLayout>

最后活动,为了测试这个,我使用了MainActivity,所以 MainActicity.java 是: -

导入android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Spinner;

import java.util.ArrayList; import java.util.Arrays;

/ **  *由Mike092015于2016年6月3日创建。  * / 公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner DistrictBetterSpinner;
    String[] District_Spinner = new String[]{"Punjab", "Rawalpindi", "Attock", "Faisalabad", "Chakwal", "Bhakkar", "Jhelum", "Multan", "Chiniot"
            , "Bhawalpur", "Jhelum", "Gujrat", "Gujranwala", "Lahore", "Mianwali", "Khyber Pakhtunkhwa (KPK)", "Abbotabad", "Haripur", "Kohat",
            "Sindh", "Hyderabad", "Jacobabad", "Balochistan", "Awaran", "Gawadar", "Federally Administered Tribal Areas", "Khyber Agency",
            "Mohmat Agency", "Azad Jammu and Kashmir", "Neelum", "Bagh", "Gilgit Baltistan", "Skardu", "Astore"};
    ArrayList<String> spinnerlist;
    MyArrayAdapter arrayadapter;

    DistrictBetterSpinner = (Spinner) findViewById(R.id.districtSpinner);
    spinnerlist = new ArrayList<>(Arrays.asList(District_Spinner));
    arrayadapter = new MyArrayAdapter(this,R.layout.spinner_item,spinnerlist);
    arrayadapter.setDropDownViewResource(R.layout.spinner_item);
    DistrictBetterSpinner.setAdapter(arrayadapter);
}

}

请注意,有一些更改,但它非常基于您的代码。