2中方法实现对话框

   用按钮代替Spinner,弹出的对话框,截图和代码请见附件!

 

用spinner实现对话框

public class SpinnerUseInArea extends Activity {
	Spinner mySpinner;
	private static final String[] countriesStr = { "111", "222", "333", "444" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mySpinner = (Spinner) findViewById(R.id.mySpinner);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, countriesStr);
		adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mySpinner.setAdapter(adapter);

	}
}

 

用按钮实现

public class EX03_12 extends Activity 
{
  private Button mButton1;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mButton1 = (Button) findViewById(R.id.myButton1);
    mButton1.setOnClickListener(new OnClickListener()
    {
      
      public void onClick(View v)
      {
        new AlertDialog.Builder(EX03_12.this)
       // .setTitle("niaho")
        .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                /* User clicked so do some stuff */
               /* String[] items = getResources().getStringArray(R.array.select_dialog_items);
                new AlertDialog.Builder(EX03_12.this)
                        .setMessage("You selected: " + which + " , " + items[which])
                        .show();*/
            }
        }).show();
        
        
      }
    });
  }
}

 

你可能感兴趣的:(android)