Chinaunix首页 | 论坛 | 博客
  • 博客访问: 687033
  • 博文数量: 118
  • 博客积分: 2933
  • 博客等级: 少校
  • 技术积分: 1779
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-27 10:16
文章存档

2013年(35)

2012年(27)

2011年(23)

2010年(33)

分类: Android平台

2013-04-03 10:06:49

本文来自http://blog.chinaunix.net/uid-24343152-id-3562706.html ,引用必须注明出处!



这次要说的是AlertDialog,这种对话框会经常遇到。AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow。

       先贴出程序运行的截图:
主Activity
MainActivity.java
package com.example.testandroid;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
//下拉
public class MainActivity extends Activity {
LinearLayout layout;
//Builder builder;
GridView gridView;
private MyAdapter adapter;
private AlertDialog dialog;
private int index = 0;
TextView textView;
Button btnShowDialog;
Button btnShowDialog_Layout;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new MyAdapter(this);
        setContentView(R.layout.activity_main);
        LayoutInflater inflater = LayoutInflater.from(this);
        layout = (LinearLayout) inflater.inflate(R.layout.bank_grid, null);
        gridView = (GridView) layout.findViewById(R.id.grid);
        gridView.setAdapter(adapter);
        btnShowDialog=(Button)this.findViewById(R.id.Button01);
        btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02);
        textView = (TextView) findViewById(R.id.textview);        
        btnShowDialog.setOnClickListener(new OnClickListener() {
          public void onClick(View arg0) {
         /*  // if(dialog == null){          
             //dialog = builder.show();
            }
            dialog.show();
          }*/
          showDialog_Layout(MainActivity.this);
          }
         });
        
        
        //定义按钮
      /*  btnShowDialog=(Button)this.findViewById(R.id.Button01);
      //  btnShowDialog.setOnClickListener(new ClickEvent());
        btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02);
       // btnShowDialog_Layout.setOnClickListener(new ClickEvent());
*/   
    gridView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView arg0, View arg1, int position,
        long arg3) {
String[] citys = { "1", "2", "3", "4", "5", "6", "7", "8" };
btnShowDialog.setText(citys[position]);
dialog.dismiss();
adapter.map.put(index, false);
adapter.map.put(position, true);
index = position;
//adapter.notifyDataSetChanged();
      }
     });
    }
    
    //统一处理按键事件
    class ClickEvent implements OnClickListener{
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==btnShowDialog)
    showDialog(MainActivity.this);
   
    else if(v==btnShowDialog_Layout)
    showDialog_Layout(MainActivity.this);
   
    }


    }


    //显示基本的AlertDialog
private void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("Button1",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的Button1");
}
});
builder.setNeutralButton("Button2",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的Button2");
}
});
builder.setNegativeButton("Button3",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的Button3");
}
});
builder.show();
}




    //显示基于Layout的AlertDialog
private void showDialog_Layout(Context context) {
LayoutInflater inflater = LayoutInflater.from(this);
//final View textEntryView = inflater.inflate(R.layout.dialoglayout, null);
//final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setView(layout);
builder.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// setTitle(edtInput.getText());
}
});
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//setTitle("");
}
});
if (dialog == null) {
dialog = builder.show();
} else {
dialog.show();
}
}
}
自定义适配器
MyAdapter.java

package com.example.testandroid;


import java.util.HashMap;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;


public class MyAdapter extends BaseAdapter {
private Context context;
private String[] citys={"1","2","3","4","5","6","7","8"};
private LayoutInflater inflater;
public HashMap map;
 
public MyAdapter(Context context) {
super();
this.context = context;
inflater = LayoutInflater.from(context);
map = new HashMap();
for (int i = 0; i < citys.length; i++) {
map.put(i, false);
}
}
@Override
public int getCount() {
return citys.length;
}
@Override
public Object getItem(int paramInt) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int paramInt) {
// TODO Auto-generated method stub
return paramInt;
}
@Override
public View getView(int position, View view, ViewGroup paramViewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.bank_item, null);
}
TextView textView = (TextView) view.findViewById(R.id.list_text);
RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutton);
radioButton.setChecked(map.get(position));
textView.setText(citys[position]);
return view;
}
}

activity_main.xml
    xmlns:tools=""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


            android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="非Layout型对话框" >
   


            android:id="@+id/Button02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Layout型对话框" >
   


            android:id="@+id/View01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
   
            android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />


bank_grid.xml

    android:id="@+id/grid_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:orientation="vertical"
    android:padding="20dp" >


            android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:horizontalSpacing="10px"
        android:numColumns="4"
        android:scrollbars="vertical"
        android:verticalSpacing="20px" />

bank_item.xml

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
     >
              android:id="@+id/radiobutton"
        android:layout_width="2dp"
        android:layout_height="2dp"
        android:layout_marginRight="15dp"
        android:focusable="false"
        android:clickable="false"
        
        android:focusableInTouchMode="false"
        />
            android:id="@+id/list_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000" 
        android:layout_gravity="center_vertical"/>








阅读(1291) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~