java 如何为微调器设置“提示文本”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37810610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:50:07  来源:igfitidea点击:

How to set "hint text" for spinner?

javaandroidspinnerandroid-spinner

提问by Karthi

The value of the Spinner from Database. I am confusing to add hint for Spinner, I followed some posts in SO but i am struck with this place and I Tried this

数据库中 Spinner 的值。我很困惑为 Spinner 添加提示,我关注了 SO 中的一些帖子,但我对这个地方感到震惊,我尝试了这个

    Spinner sp = (Spinner)findViewById(R.id.spinner); 
    sp.setSelection(pos);

the position have Integer value so i give it as a Integer value for position,also i try with getCount() inside of setSeletction() method

位置有整数值,所以我把它作为位置的整数值,我也尝试在 setSeletction() 方法中使用 getCount()

main.java

主程序

    //spinner for customer
        sp=(Spinner)findViewById(R.id.spinner);
        adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
        sp.setAdapter(adapter);
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sales_order);
        cus_name = (Spinner) findViewById(R.id.spinner);//customer spinner

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        //spinner for customer
        sp=(Spinner)findViewById(R.id.spinner);
        adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
        sp.setAdapter(adapter);


    }

private class BackTask extends AsyncTask<Void,Void,Void> {
        ArrayList<String> list;
        protected void onPreExecute(){
            super.onPreExecute();
            list=new ArrayList<>();
        }
        protected Void doInBackground(Void...params){
            InputStream is=null;
            String result="";
            try{
                HttpClient httpclient=new DefaultHttpClient();
                HttpPost httppost= new HttpPost("http://IP/web_services/customer_spinner.php");
                HttpResponse response=httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                // Get our response as a String.
                is = entity.getContent();
            }catch(IOException e){
                e.printStackTrace();
            }

            //convert response to string
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    result+=line;
                }
                is.close();
                //result=sb.toString();
            }catch(Exception e){
                e.printStackTrace();
            }
            // parse json data
            try{
                JSONObject object = new JSONObject(result);
                JSONArray jArray = object.getJSONArray("cus_Name");
                //JSONArray jArray =new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    JSONObject jsonObject=jArray.getJSONObject(i);
                    // add interviewee name to arraylist
                    list.add(jsonObject.getString("cus_Name"));
                }
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void result){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

回答by Harshad Pansuriya

There are lot of way :

有很多方法:

1.)Android Hint Spinner

1.) Android 提示微调器

Use this dependency in build.gradle file.

在 build.gradle 文件中使用此依赖项。

dependencies {
    compile 'me.srodrigo:androidhintspinner:1.0.0'
}

Output :

输出 :

enter image description here

在此处输入图片说明

for more detail visit this : https://github.com/srodrigo/Android-Hint-Spinner

有关更多详细信息,请访问:https: //github.com/srodrigo/Android-Hint-Spinner

2.) second Way

2.) 第二种方式

visit here : https://github.com/ravivyas84/AndroidSpinnerHint

访问这里:https: //github.com/ravivyas84/AndroidSpinnerHint