我是android的新手并且在具有图像和文本的自定义微调器中搜索如何添加提示(无法选择)。虽然我发现它用于简单的微调器,但我无法实现它自定义微调器。 我的代码:
package com.example.hp.login_stratup_labs;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class RegistrationActivity extends Activity {
String[] codes = {"Select Country", "India - +91", "United States of America - +1"};
// Declaring the Integer Array with resourse Id's of Images for the Spinners
int[] flags = {0, R.drawable.f094, R.drawable.f222};
Spinner country;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
Spinner countrycode=(Spinner)findViewById(R.id.registerCountryCode);
countrycode.setAdapter(new MyAdapter(RegistrationActivity.this, R.layout.custom, codes));
}
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, 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(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.custom, parent, false);
TextView label=(TextView)row.findViewById(R.id.txt);
label.setText(codes[position]);
ImageView flag_image=(ImageView)row.findViewById(R.id.img);
flag_image.setImageResource(flags[position]);
return row;
}
}
}
答案 0 :(得分:-1)
你可以这样做
主要活动中的
String[] codes = {"India - +91", "United States of America - +1","Select Country"};
MyAdapter adapter = new MyAdapter(RegistrationActivity.this, R.layout.custom, codes);
Spinner countrycode=(Spinner)findViewById(R.id.registerCountryCode);
countrycode.setAdapter(adapter);
countrycode.setSelection(adapter.getCount());
将此方法添加到
@Override
public int getCount() {
return codes.length()-1;
}