2012-02-23 25 views
1

私は小さなアプリケーションを作っています。私はスピナーを持っています。今は、アプリケーションが読み込まれるときのスピナーのテキストとしてのスピナーの最初のオプションを示しています。アプリケーションを読み込むときにヒントのような特別なテキストを表示したいのですが。しかし、あなたがスピナーを押してオプションを得るとき、私はそれがオリジナルのオプションを示すのが大好きです。今のように、アプリケーションをロードするとオプション番号0が表示されますが、その上にテキストが他の文字列として表示されます。アンドロイドスピナーにヒントを入れる

これは可能ですか?

+0

トラブル:

は、ここでの活動は次のようになりますか? –

+0

最後の部分に書かれたように; "今のように、アプリケーションをロードするとオプション番号0が表示されますが、その上のテキストに他の文字列が表示されます。 – Loyalar

+0

インデックス値ではなく文字列値をスピナーに表示する方法を尋ねていますか? – Woodsy

答えて

1

は今、0が選択されていると仮定しかし、アプリケーションのロードは、それがonItemSelected方法で

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

        @Override 
        public void onItemSelected(AdapterView<?> arg0, View arg1, 
          int arg2, long arg3) { 
         spinner.setSelection(2); 

        } 

        @Override 
        public void onNothingSelected(AdapterView<?> arg0) { 
         // TODO Auto-generated method stub 

        } 
       }); 
+0

HmmというSpinnerのための 'android:hint'属性はありません(EditTextのように)。それは実際には機能しますが、あなたはまだトップセレクションを選択することができます。あなたがそれを選択できないようにスピナーの項目を無効にする方法はありますか? – Loyalar

3

で選択、他の文字列を設定したとき、私は代わりに、スピナーのボタンを使用してこれを処理します。 I have the sample project up on GitHub.

私はSpinnerとボタンが実際に同じに見えることを示すためにSpinnerとボタンの両方を表示しています。ボタンを除いて、あなたが望むものであれば、最初のテキストを設定することができます。あなたが欲しいものを説明する

package com.stevebergamini.spinnerbutton; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Spinner; 

public class MainActivity extends Activity { 

    Spinner spinner1; 
    Button button1; 
    AlertDialog ad; 
    String[] countries; 

    int selected = -1; 

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

     spinner1 = (Spinner) findViewById(R.id.spinner1); 
     button1 = (Button) findViewById(R.id.button1); 

     countries = getResources().getStringArray(R.array.country_names); 

     // You can also use an adapter for the allert dialog if you'd like 
     // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries);   

     ad = new AlertDialog.Builder(MainActivity.this).setSingleChoiceItems(countries, selected, 
       new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          button1.setText(countries[which]); 
          selected = which; 
          ad.dismiss(); 

         }}).setTitle(R.string.select_country).create(); 


     button1.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       ad.getListView().setSelection(selected); 
       ad.show();    
      }}); 

    } 

} 
関連する問題