用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

Spinner 菜单的动态添加与删除

2012-09-05 作者: 神马举报

[android]代码库

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class EX04_09 extends Activity {
	private static final String[] countriesStr = { "春节", "元宵节", "植树节", "清明节" };
	private TextView myTextView;
	private EditText myEditText;
	private Button myButton_add;
	private Button myButton_remove;
	private Spinner mySpinner;
	private ArrayAdapter adapter;
	private List allCountries;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		allCountries = new ArrayList();
		for (int i = 0; i < countriesStr.length; i++) {
			allCountries.add(countriesStr[i]);
		}
		/* new ArrayAdapter物件并将allCountries传入 */
		adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,
				allCountries);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		myTextView = (TextView) findViewById(R.id.myTextView);
		myEditText = (EditText) findViewById(R.id.myEditText);
		myButton_add = (Button) findViewById(R.id.myButton_add);
		myButton_remove = (Button) findViewById(R.id.myButton_remove);
		mySpinner = (Spinner) findViewById(R.id.mySpinner);
		/* 将ArrayAdapter加入Spinner对象中 */
		mySpinner.setAdapter(adapter);
		/* 将myButton_add加入OnClickListener */
		myButton_add.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String newCountry = myEditText.getText().toString();
				/* 先比对新增的值是否已存在,不存在才可新增 */
				for (int i = 0; i < adapter.getCount(); i++) {
					if (newCountry.equals(adapter.getItem(i))) {
						return;
					}
				}
				if (!newCountry.equals("")) {
					adapter.add(newCountry);
					/* 取得新增的值的位置 */
					int position = adapter.getPosition(newCountry);
					/* 将Spinner选取在新增的值的位置 */
					mySpinner.setSelection(position);
					/* 将myEditText清空 */
					myEditText.setText("");
				}
			}
		});
		/* 将myButton_remove加入OnClickListener */
		myButton_remove.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mySpinner.getSelectedItem() != null) {
					/* 移除mySpinner的值 */
					adapter.remove(mySpinner.getSelectedItem().toString());
					/* 将myEditText清空 */
					myEditText.setText("");
					if (adapter.getCount() == 0) {
						/* 将myTextView清空 */
						myTextView.setText("");
					}
				}
			}
		});
		/* 将mySpinner加入OnItemSelectedListener */
		mySpinner
				.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView arg0, View arg1,
							int arg2, long arg3) {
						/* 将所选mySpinner的值带入myTextView中 */
						myTextView.setText(arg0.getSelectedItem().toString());
					}

					@Override
					public void onNothingSelected(AdapterView arg0) {

					}
				});
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...