android > ListView 点击弹出窗

先是 弹出窗  Dialog  的函数, 一个 是 弹出 输入窗口, 一个是弹出选择 窗口  

package gloabal.mft;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

import set.mft.SetWendu;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.widget.EditText;
import android.widget.SimpleAdapter;




public class Other {
	public static void main(String[] args) {
		
	}
	
	public void alertInput(Context content,int type,final ArrayList<HashMap<String, Object>> listItem,final int position,final SimpleAdapter listItemAdapter,String notice,final String ItemName){
		
		final EditText inputText = new EditText(content); 
		if(type == 1){
			inputText.setInputType(InputType.TYPE_CLASS_NUMBER);
		}
		
		
		Builder builder = new Builder(content);
        builder.setTitle(notice);  
        builder.setIcon(android.R.drawable.ic_dialog_info);  			                 
        builder.setView(inputText);  
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {       
            public void onClick(DialogInterface dialog, int id) {    
                Appendable value = inputText.getText(); 
                String value_str = value.toString();
                
                try {
					value_str = subStr(value_str, 10);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
                //v1.setText(value.toString());  
                listItem.remove(position);
                HashMap<String, Object> map = new HashMap<String, Object>();
	            map.put("ItemName", ItemName);  
	            map.put("ItemValue", value_str);  
	            map.put("ItemIcon", android.R.drawable.arrow_down_float);
	            listItem.add(position,map); 
	            listItemAdapter.notifyDataSetChanged();
	            dialog.dismiss();
            }       
        });  
        builder.setNegativeButton("取消", null);  
        builder.show(); 		
		
	}
	public void alertSigleChoose(Context content, final String[] chooseValue,final ArrayList<HashMap<String, Object>> listItem,final int position,final SimpleAdapter listItemAdapter,String notice,final String ItemName){
		final EditText inputText = new EditText(content); 
		Builder builder = new Builder(content);
        builder.setTitle(notice);  
        builder.setIcon(android.R.drawable.ic_dialog_info); 
        builder.setSingleChoiceItems(chooseValue, 0,     
                  new DialogInterface.OnClickListener() {                      
             public void onClick(DialogInterface dialog, int which) {    
            	String value = "";
            	
            	value = chooseValue[which];
            	
                listItem.remove(position);
                HashMap<String, Object> map = new HashMap<String, Object>();
	            map.put("ItemName", ItemName);  
	            map.put("ItemValue", value.toString());  
	            map.put("ItemIcon", android.R.drawable.arrow_down_float);
	            listItem.add(position,map); 
	            listItemAdapter.notifyDataSetChanged();			                          
                dialog.dismiss();  //关闭对话框  
             }    
          }    
        );    
        builder.show();	
	}	
	public static String subStr(String str, int subSLength)    
            throws UnsupportedEncodingException{   
        if (str == null)    
            return "";    
        else{   
            int tempSubLength = subSLength;//截取字节数  
            String subStr = str.substring(0, str.length()<subSLength ? str.length() : subSLength);//截取的子串    
            int subStrByetsL = subStr.getBytes("GBK").length;//截取子串的字节长度   
            //int subStrByetsL = subStr.getBytes().length;//截取子串的字节长度   
            // 说明截取的字符串中包含有汉字    
            while (subStrByetsL > tempSubLength){    
                int subSLengthTemp = --subSLength;  
                subStr = str.substring(0, subSLengthTemp>str.length() ? str.length() : subSLengthTemp);    
                subStrByetsL = subStr.getBytes("GBK").length;  
                //subStrByetsL = subStr.getBytes().length;  
            }    
            return subStr;   
        }  
    }  	
	
	//
}

然后是 调用 

other.alertInput(SetWendu.this, 0,listItem, position, listItemAdapter, "请输入", "名称");

其中 listItem 

			listItem = new ArrayList<HashMap<String,Object>>();
			for(int i=0;i<10;i++){  
	            HashMap<String, Object> map = new HashMap<String, Object>();
	            map.put("ItemName", "aaa");  
	            listItem.add(map);  
	        } 

listItemAdapter

			final SimpleAdapter listItemAdapter = new SimpleAdapter(SetWendu.this, listItem,   
	                R.layout.set_in_list,   
	                new String[]{"ItemName","ItemValue","ItemIcon"},   
	                new int[]{R.id.set_list_left,R.id.set_list_right,R.id.set_list_icon}  
	        );

listItemAdapter 点击事件

    		list.setOnItemClickListener(new OnItemClickListener() {  
	            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
	                    long arg3) {  
	                // TODO Auto-generated method stub  
	            	final int position = arg2;
	            };
	                
			});

 -------------------------- 弹出 seekbar  加载 view  -- start -----------------------------------

package t3.com;





import java.io.File;
import java.io.IOException;




import android.app.Activity;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;  
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class T3Activity extends Activity {
    /** Called when the activity is first created. */
	private Button bu;
	private Context content;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        content = T3Activity.this;
        bu = (Button)findViewById(R.id.bu);
        bu.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//
				LayoutInflater mInflater = LayoutInflater.from(content);
				View view = null;				
				view = mInflater.inflate(R.layout.alertlayout, null);				
				final TextView textview = (TextView)view.findViewById(R.id.v1);
				final SeekBar seekbar = (SeekBar)view.findViewById(R.id.seekBar1);
				
				textview.setText("50");
				seekbar.setProgress(50);
				
				seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {  
		              
		            //当停止时  
		            public void onStopTrackingTouch(SeekBar arg0) {  
		                // TODO Auto-generated method stub  
		            	textview.setText(""+seekbar.getProgress());  
		            }  
		              
		            //当开始拉动时  
		            public void onStartTrackingTouch(SeekBar arg0) {  
		                // TODO Auto-generated method stub  
		            	textview.setText(""+seekbar.getProgress());  
		                  
		            }  
		              
		            //当拉动条 改变值时  
		            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {  
		                // TODO Auto-generated method stub  
		            	textview.setText(""+seekbar.getProgress());  
		                  
		            }  
		        }); 
				
				
				final Builder builder = new Builder(content);
				builder.setTitle("选择值");				
		        builder.setIcon(android.R.drawable.ic_dialog_info);  			                 
		        builder.setView(view);  
		        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {       
		            public void onClick(DialogInterface dialog, int id) {    
		                
			            dialog.dismiss();
		            }       
		        });  
		        builder.setNegativeButton("取消", null);  
		        builder.show();	
		        
				//\\
			}
		});
        
        
      
    }      
    //\\
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/v1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 -------------------------- 弹出 seekbar  加载 view  -- end -----------------------------------

猜你喜欢

转载自mft.iteye.com/blog/1616808
今日推荐