android 列表框

1、activity_main.xml



    

2、ActivityMain.class类

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.Toast;
类的实现

public class MainActivity extends Activity {
	private Button btn1;

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

		final String[] nItems = {"item1","item2","item3", "item4", "item5", "item6"};
		
		btn1 = (Button) findViewById(R.id.button1);
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				
				builder.setTitle("列表选择框");
				builder.setItems(nItems, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						//	显示提示信息
						Toast toast = Toast.makeText(getApplicationContext(), 
								"你选择的Id为"+arg1+",值为"+nItems[arg1], Toast.LENGTH_SHORT);
						toast.show();
					}
				});
				
				builder.create().show();
			}
		});
	}
}

3、运行图

android 列表框_第1张图片


你可能感兴趣的:(Android,android,列表框)