Android项目笔记五:带自己布局文件的AlertDialog窗口

来源:互联网 发布:算法实现是什么 编辑:程序博客网 时间:2024/04/28 00:39

窗口的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_wifi_password_TableLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!--第一行-->
    <TableRow
        android:id="@+id/my_wifi_password_TableRow1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码:"
                style="@style/my_socket_tcp_text_style">


            </TextView>


            <EditText
                android:id="@+id/my_wifi_password_EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:singleLine="true"
                android:textColor="#8B0000"
                android:inputType="text"
                android:text="">
            </EditText>
        </LinearLayout>


    </TableRow>


<!--    &lt;!&ndash;第二行&ndash;&gt;
    <TableRow
        android:id="@+id/my_wifi_password_TableRow2"
        android:orientation="horizontal"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1">


            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="取消"
                android:layout_weight="1">


            </Button>


            <Button
                android:layout_width="wrap_content"
                android:text="确定"
                android:layout_height="match_parent"
                android:layout_weight="1">


            </Button>
        </LinearLayout>


    </TableRow>-->


</TableLayout>



在listview的监听事件中:

my_listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){



    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        // TODO Auto-generated method stub
        HashMap<String, String> my_map = (HashMap<String, String>) my_listview.getItemAtPosition(arg2);
        String title = my_map.get("title");
        String content = my_map.get("itemContent");


        if(title.equals("扫描网络")){
            handler.postDelayed(RefreshWifiList, 20);
            return;
        }


        MySocketActivity.this.user_select_wifiname=title;
        LayoutInflater inflater=getLayoutInflater();
        View    layout=inflater.inflate(R.layout.my_wifi_password,
                (ViewGroup)findViewById(R.id.my_wifi_password_TableLayout));
        MySocketActivity.this.passWord_EditText = (EditText)layout.findViewById(R.id.my_wifi_password_EditText);
        AlertDialog my_DialogPassWord = new AlertDialog.Builder(MySocketActivity.this).create();
        my_DialogPassWord.setTitle("请输入密码");
        my_DialogPassWord.setView(layout);


        my_DialogPassWord.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MySocketActivity.this,"选择了取消按钮",Toast.LENGTH_SHORT).show();
            }
        });
        my_DialogPassWord.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {


                /*Toast.makeText(MySocketActivity.this,user_select_wifipassword,Toast.LENGTH_SHORT).show();*/
                if(null != passWord_EditText) {
                    MySocketActivity.this. user_select_wifipassword=passWord_EditText.getText().toString();
                    if(user_select_wifipassword.equals("")){
                        MySocketActivity.this.user_select_wifipassword="null";
                    }
                 //   if(!my_wificlass.isWiFiActive(MySocketActivity.this)) {
                        if(my_timer_state == 0) {
                            Toast.makeText(MySocketActivity.this,"正在连接-->"+user_select_wifiname,Toast.LENGTH_SHORT).show();
                            handler.postDelayed(runnable, 20);
                        }
                 //   }


                }
            }
        });
        my_DialogPassWord.show();
    }

});

注意:

MySocketActivity.this.passWord_EditText = (EditText)layout.findViewById(R.id.my_wifi_password_EditText);

一定要在

        LayoutInflater inflater=getLayoutInflater();
        View    layout=inflater.inflate(R.layout.my_wifi_password,
                (ViewGroup)findViewById(R.id.my_wifi_password_TableLayout));

后面,否则passWord_EditText 为null


wifi类别刷新:

public void my_Refresh_wifi_list(){
    my_list_wifi=my_wificlass.my_WifiList(MySocketActivity.this);
    List<Map<String,Object>> my_list = new ArrayList<Map<String, Object>>();
    for (int i=0;i<my_list_wifi.size();i++){
        Map<String,Object> map=new HashMap<String,Object>();
        if(Math.abs(my_list_wifi.get(i).level)>80){
            map.put("image",my_imageld[1]);
        }else if(Math.abs(my_list_wifi.get(i).level)>60){
            map.put("image",my_imageld[2]);
        }else if(Math.abs(my_list_wifi.get(i).level)>40){
            map.put("image",my_imageld[3]);
        }else {
            map.put("image",my_imageld[4]);
        }
        map.put("title",my_list_wifi.get(i).SSID);


        if(my_list_wifi.get(i).capabilities.contains("WPA")){
            map.put("KwyType","WPA加密");
        }else if(my_list_wifi.get(i).capabilities.contains("WEP")){
            map.put("KwyType","WEP加密");
        }else {
            map.put("KwyType","无密码");
        }


        my_list.add(map);
    }
    //添加扫描按钮
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("image",my_imageld[0]);
    map.put("title","扫描网络");
    map.put("KwyType","。。。。。。");
    my_list.add(map);


    SimpleAdapter adapter=new SimpleAdapter(MySocketActivity.this,my_list,R.layout.my_socket_wifi_list_items,
            new String[]{"title","KwyType","image"},
            new int[]{R.id.my_socket_wifi_list_items_WifiName,
                    R.id.my_socket_wifi_list_items_WifKeyType,
                    R.id.my_socket_wifi_list_items_ImageView});
    my_listview.setAdapter(adapter);
}

wifi的list下节在讲解


0 0
原创粉丝点击