我正在使用MaterialBetterSpinner
,我想显示国家/地区标志,国家/地区代码和国家/地区名称。它正确地在简单的微调器中工作。但是,当我使用MaterialBetterSpinner
时,它会在showDropDown()
方法中显示错误。我怎样才能在android中解决这个错误?
**This is my MainActivity**
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
protected SparseArray<ArrayList<Country>> mCountriesMap = new SparseArray<ArrayList<Country>>();
protected EditText inputName, inputEmail, inputPassword, inputDob, inputMobile;
private TextInputLayout inputLayoutName, inputLayoutEmail, inputLayoutPassword, inputLayoutDob, inputLayoutmobile, inputLayoutCountry;
private Button btnSignUp;
private ImageView dp;
CountryAdapter mAdapter;
int yearc, monthc, dayc;
ArrayList<Country> arrayList;
static final int DIALOG_ID = 0;
//@Bind(R.id.input_country)
MaterialBetterSpinner spcountry;
@Bind(R.id.country_code)
MaterialBetterSpinner countrycode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new VerifyPhoneFragment())
.commit();
}*/
ButterKnife.bind(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name);
inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email);
inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);
inputLayoutDob = (TextInputLayout) findViewById(R.id.input_layout_dob);
inputLayoutmobile = (TextInputLayout) findViewById(R.id.input_layout_mobile);
inputLayoutCountry = (TextInputLayout) findViewById(R.id.input_layout_country);
inputName = (EditText) findViewById(R.id.input_name);
inputEmail = (EditText) findViewById(R.id.input_email);
inputPassword = (EditText) findViewById(R.id.input_password);
inputDob = (EditText) findViewById(R.id.input_dob);
inputMobile = (EditText) findViewById(R.id.input_mobile);
dp = (ImageView) findViewById(R.id.calender);
btnSignUp = (Button) findViewById(R.id.btn_signup);
inputName.addTextChangedListener(new MyTextWatcher(inputName));
inputEmail.addTextChangedListener(new MyTextWatcher(inputEmail));
inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));
inputDob.addTextChangedListener(new MyTextWatcher(inputDob));
inputMobile.addTextChangedListener(new MyTextWatcher(inputMobile));
spcountry= (MaterialBetterSpinner) findViewById(R.id.input_country);
final Calendar cal = Calendar.getInstance();
yearc = cal.get(Calendar.YEAR);
monthc = cal.get(Calendar.MONTH);
dayc = cal.get(Calendar.DAY_OF_MONTH);
showDatePickerOndp();
String[] list = getResources().getStringArray(R.array.country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, list);
countrycode.setAdapter(adapter);
arrayList = loadData();
mAdapter = new CountryAdapter(this,arrayList);
spcountry.setAdapter(mAdapter);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm();
}
});
}
public void showDatePickerOndp(){
dp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
});
}
public ArrayList<Country> loadData() {
ArrayList<Country> data = new ArrayList<Country>(233);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(getApplicationContext().getAssets().open("countries.dat"), "UTF-8"));
Country country = null;
String line;
int i = 0;
while ((line = reader.readLine()) != null) {
country = new Country(this, line, i);
data.add(country);
i++;
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
**This is my MaterialBetterSpinner Class**
public class MaterialBetterSpinner extends MaterialAutoCompleteTextView implements AdapterView.OnItemClickListener {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
private boolean isPopup;
public MaterialBetterSpinner(Context context) {
super(context);
setOnItemClickListener(this);
}
public MaterialBetterSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
setOnItemClickListener(this);
}
public MaterialBetterSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
setOnItemClickListener(this);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering("", 0);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
setKeyListener(null);
dismissDropDown();
} else {
isPopup = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration < MAX_CLICK_DURATION) {
if (isPopup) {
dismissDropDown();
isPopup = false;
} else {
requestFocus();
//showDropDown();
isPopup = true;
}
}
}
}
return super.onTouchEvent(event);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
isPopup = false;
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
Drawable dropdownIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_expand_more_black_18dp);
if (dropdownIcon != null) {
right = dropdownIcon;
right.mutate().setAlpha(66);
}
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
}
}
**
这是我的Activity_main.xml
**
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="60dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="@string/hint_name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/hint_email" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/hint_password" />
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_dob"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:inputType="text|date"
android:hint="@string/hint_dob" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/calender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:cropToPadding="false"
android:contentDescription="@string/selectdate"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/calendar_icon" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<info.androidhive.floatinglabels.MaterialBetterSpinner
android:id="@+id/country_code"
android:layout_width="100dp"
android:layout_height="41dp"
android:layout_alignParentStart="true"
android:hint="@string/hint_mobile"
android:dropDownWidth="200dp"
/>
</android.support.design.widget.TextInputLayout>
<EditText
android:id="@+id/input_mobile"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingTop="27dp"
android:inputType="text|phone"
/>
</RelativeLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_country"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<info.androidhive.floatinglabels.MaterialBetterSpinner
android:id="@+id/input_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_country" />
</android.support.design.widget.TextInputLayout>
<Button android:id="@+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_sign_up"
android:background="@color/colorPrimary"
android:layout_marginTop="40dp"
android:textColor="@android:color/white"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>