如何将值设置为Spinner

时间:2012-07-25 16:24:47

标签: android android-layout android-widget

当我选择Spinner文字时,Dialog box会显示EditText。我希望Display EditText字符串为Spinner

代码

private void Showdialog() { 
  myDialog = new Dialog(this.context); 
  myDialog.setContentView(R.layout.mydialog); 
  myDialog.setTitle("hh"); 
  myDialog.setCancelable(true);
  editText=(EditText)findViewById(R.id.editText1); 
  Button button = (Button) myDialog.findViewById(R.id.but); 
  // EditText editText=(EditText)findViewById(R.id.EditText01); 
  button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
        clickAction();   
     }   
  }); 
  myDialog.show(); 
  } 
  void clickAction() { 
     myDialog.cancel(); 
     mySpinner.setTag(editText.getText().toString());    
  }

和微调代码

  mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {  
         String cardStatusString = parent.getItemAtPosition(pos).toString();  
         if(cardStatusString.equals("Other")) { 
             Showdialog(); 
         } 
     } 
     public void onNothingSelected(AdapterView<?> arg0) { 
         // TODO Auto-generated method stub 
     } });

1 个答案:

答案 0 :(得分:1)

试试这个

public class MainActivity extends Activity {

ArrayList<String> list = new ArrayList<String>();
private Context context;
ArrayAdapter<String> adapter;

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

    context = this;
    list.add("Mercury");
    list.add("Venus");
    list.add("Earth");
    list.add("Mars");
    list.add("Jupiter");

    Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
    // Create an ArrayAdapter using the string array and a default spinner
    // layout
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Showdialog();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });
}

private void Showdialog() {
    final AlertDialog myDialog = new AlertDialog.Builder(this).create();
    // myDialog.setContentView(R.layout.mydialog);
    myDialog.setTitle("hh");
    myDialog.setCancelable(true);
    final EditText text = new EditText(this);
    myDialog.setView(text);
    myDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            String entered_name = text.getText().toString();
            list.add(entered_name);
            adapter.notifyDataSetChanged();
            // addrow(entered_name,click,time_count);
            return;
        }
    });
    myDialog.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}