彈窗常用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() {
@Override
public 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;
}
@Override
public 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相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11