Android彈窗中的spinner異常解決辦法

來源:互聯網
上載者:User

標籤:android   spinner   spinneradapter   

彈窗常用AlertDialog來襪,因為AlertDialog可以很方便的添加所要的項。比如確定/取消按鈕,自訂的標題列、自訂的資訊布局等。在自訂布局時,比如我們採用LinearLayout來布局,然後再往LinearLayout中添加一些項,比如TextView、EditText等。這些都很容易實現。

不過,如果添加spinner,可能就會出問題了。找了很久,發現是context引起的。因為spinner在點擊展開下拉式清單時,需要依賴於表單,而使用context只是提供了一個上下文,並沒有提供表單,所以會造成錯誤。下面是已實現的代碼:

package com.sparkle;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.Color;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.Spinner;import android.widget.SpinnerAdapter;import android.widget.TextView;public class Contury {private Activity _activity = null;private Context _context = null;private Spinner _spinner = null;public Contury(Activity activity) {_activity = activity;_context = activity;}private LinearLayout GenerateLinearLayout(int orientation) {LinearLayout linearLayout = new LinearLayout(_context);linearLayout.setPadding(16, 16, 16, 16);linearLayout.setOrientation(orientation);return linearLayout;}@SuppressWarnings("deprecation")private Spinner GenerateSpinner() {Spinner spinner = new Spinner(_activity);LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);spinner.setLayoutParams(layoutParams);return spinner;}public void Popup() {Builder alertDialogBuilder = new AlertDialog.Builder(_activity);View view = GenerateLayout();alertDialogBuilder.setView(view);alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Log.e("TEST","OK");}});alertDialogBuilder.setNegativeButton("Cancel", null);Dialog alertDialog = alertDialogBuilder.create();alertDialog.show();}private View GenerateLayout() {LinearLayout linearLayout = GenerateLinearLayout(LinearLayout.VERTICAL);LinearLayout messageLinearLayout = GenerateMessageLayout();linearLayout.addView(messageLinearLayout);return linearLayout;}private LinearLayout GenerateMessageLayout() {LinearLayout linearLayout = GenerateLinearLayout(LinearLayout.HORIZONTAL);_spinner = GenerateSpinner();_spinner.setAdapter(new CityAdapter(_activity));_spinner.setSelection(0);linearLayout.addView(_spinner);return linearLayout;}private class CityAdapter extends BaseAdapter implements SpinnerAdapter {private Context _con=null;private String[] _cities=null;public CityAdapter(Context context){_con=context;_cities=_con.getResources().getStringArray(R.array.cities);}        @Override        public int getCount() {        if(_cities==null)        {        return 0;        }            return _cities.length;        }        @Override        public Object getItem(int position) {        if(_cities==null||_cities.length<=0)        {        return null;        }            return _cities[position];        }        @Override        public long getItemId(int position) {            return position;        }  @Overridepublic View getView(int position, View convertView, ViewGroup parent) {TextView textView=new TextView(_con);textView.setTextColor(Color.BLACK);textView.setTextSize(20);textView.setText(_cities[position]);return textView;}    }}
註:

1、spinner和AlertDialog的new需使用activity,使用context將在彈出下拉式功能表時造成錯誤。

2、_con.getResources().getStringArray(R.array.cities)是載入xml中定義的數組。

city.xml(位於values檔案夾下)

<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="cities">        <item>廈門</item>        <item>北京</item>        <item>上海</item>    </string-array></resources>




Android彈窗中的spinner異常解決辦法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.