当前位置:  编程技术>移动开发
本页文章导读:
    ▪蓝牙的施用        蓝牙的使用 工程结构图:[img][/img]MainActivitypackage com.zzl.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import androi.........
    ▪ popupWindow的应用        popupWindow的使用 1.定义一个Popupwindow: private PopupWindow popWinOfKeyboard; private LayoutInflater inflater ;   2.onCreate() inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate.........
    ▪ List报表       List表格 线上效果图:[img][/img]工程结构图:[img][/img]布局文件:list_header.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear.........

[1]蓝牙的施用
    来源: 互联网  发布时间: 2014-02-18
蓝牙的使用
工程结构图:
[img]

[/img]

MainActivity
package com.zzl.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button) findViewById(R.id.button1);
        
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(),BTConnectActivity.class);
				startActivity(intent);
				finish();
			}
		});
    }
}


BTConnectActivity
package com.zzl.test;

/*************************************************************/
/* Project Shmimn 
 *  Mobile Health-care Device
 *  Yuhua Chen
 *  2011-4-24 16:35:21
 */
/*************************************************************/
import java.io.IOException;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;

import android.widget.AdapterView;

import android.widget.ListView;
import android.widget.ArrayAdapter;

import android.widget.Toast;
import android.util.Log;
import android.view.View;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
public class BTConnectActivity extends Activity {

	// private static final String TAG = "BTConnectActivity";

	/** Called when the activity is first created. */
	public static final int REQUEST_ENABLE_BT = 8807;
	public BroadcastReceiver mBTReceiver;
	public static BluetoothSocket mBTSocket;
	public BluetoothAdapter mBTAdapter;
	public BluetoothDevice mBTDevice;
	private ArrayAdapter<String> adtDvcs;
	private List<String> lstDvcsStr = new ArrayList<String>();
	private ListView lvDevicesList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.btconnect);


		// 初始化 BluetoothAdapter
		mBTAdapter = BluetoothAdapter.getDefaultAdapter(); // c

		if (mBTAdapter == null) {
			Toast.makeText(BTConnectActivity.this, "没有支持蓝牙的设备! ",
					Toast.LENGTH_SHORT).show();
			this.finish();
		}
		if (!mBTAdapter.isEnabled()) {
			// Open a new dialog to ask user whether wanna open BT
			Toast.makeText(BTConnectActivity.this, "请打开手机蓝牙后再重试! ",
					Toast.LENGTH_SHORT).show();
			this.finish();

			Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enabler, REQUEST_ENABLE_BT);
		}
		// 开启 BroadCast Receiver
		mBTReceiver = new BroadcastReceiver() {
			public void onReceive(Context context, Intent intent) {
				String act = intent.getAction();
				// To see whether the action is that already found devices
				if (act.equals(BluetoothDevice.ACTION_FOUND)) {
					// 如果发现一个设备,得到设备的对象


					BluetoothDevice tmpDvc = intent
							.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
					// Put the name & address into a string
					String tmpDvcStr = tmpDvc.getName() + "|"
							+ tmpDvc.getAddress();
					if (lstDvcsStr.indexOf(tmpDvcStr) == -1) {
						// Avoid duplicate add devices
						lstDvcsStr.add(tmpDvcStr);
						adtDvcs.notifyDataSetChanged();
						Toast.makeText(BTConnectActivity.this, "发现一个新设备",
								Toast.LENGTH_SHORT).show();
					}
				}
				if (act.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
					Toast.makeText(BTConnectActivity.this, "搜索完成!",
							Toast.LENGTH_SHORT).show();
				}

				if (act.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
					Toast.makeText(BTConnectActivity.this, "开始搜索设备",
							Toast.LENGTH_SHORT).show();
				}
			}
		};

		// 注册 broadcastReceiver
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		registerReceiver(mBTReceiver, filter);
		filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
		registerReceiver(mBTReceiver, filter);
		filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		registerReceiver(mBTReceiver, filter);

		lvDevicesList = (ListView) findViewById(R.id.lvDevicesList);

		adtDvcs = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, lstDvcsStr);
		lvDevicesList.setAdapter(adtDvcs);

		if (mBTAdapter.isDiscovering()) {
			Toast.makeText(BTConnectActivity.this, "正在搜索......",
					Toast.LENGTH_SHORT).show();
		} else {
			lstDvcsStr.clear();
			adtDvcs.notifyDataSetChanged();
			mBTDevice = null;
			mBTAdapter.startDiscovery();
		}

		// 在设备列表中创建单击事件
		lvDevicesList
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						if (mBTAdapter == null)
							Toast.makeText(BTConnectActivity.this, "没有搜索到蓝牙设备",
									Toast.LENGTH_SHORT).show();
						else {
							// 停止搜索
							mBTAdapter.cancelDiscovery();
							// 得到设备的地址
							String str = lstDvcsStr.get(arg2);
							String[] dvcValues = str.split("\\|");
							String dvcAddr = dvcValues[1];
							UUID dvcUUID = UUID
									.fromString("00001101-0000-1000-8000-00805F9B34FB");
							mBTDevice = mBTAdapter.getRemoteDevice(dvcAddr);
							// 连接设备
							try {
								// 根据UUID创建并返回一个BluetoothSocket
								mBTSocket = mBTDevice
										.createRfcommSocketToServiceRecord(dvcUUID);
								mBTSocket.connect();

								Intent intent = new Intent(getApplicationContext(),WriteActivity.class);
								startActivity(intent);
								
								
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				});
	}

	public void onActivityResult(int RequestCode, int ResultCode, Intent data) {
		Log.i("BTConnectActivity", "RequestCode is " + RequestCode
				+ ", ResultCode is " + ResultCode);
		switch (RequestCode) {
		case REQUEST_ENABLE_BT:
			if (ResultCode == RESULT_OK) {
				Toast.makeText(this.getApplicationContext(), "蓝牙已连接!",
						Toast.LENGTH_SHORT).show();
			} else if (ResultCode == RESULT_CANCELED) {
				Toast.makeText(this.getApplicationContext(), "蓝牙连接被取消!",
						Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}

	@Override
	protected void onDestroy() {
		try {
			mBTSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.unregisterReceiver(mBTReceiver);
		super.onDestroy();
	}
}





WriteActivity
package com.zzl.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WriteActivity extends Activity {
    
	
	private TextView tv;
	private BTReadThread mReadThread = new BTReadThread(50);
	private boolean enRead = false; 
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:
				tv.setText(msg.obj.toString());
				break;
			}
		}
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write);
        tv = (TextView) findViewById(R.id.tv);
        btint();
        
    }
    
    
    /**
	 * 开启读取蓝牙数据的线程
	 */
	private void btint() {
		try {
			if (BTConnectActivity.mBTSocket.getInputStream() != null) {
				enRead = true;
				mReadThread.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/***
	 * 蓝牙操作
	 */
	class BTReadThread extends Thread {
		private int wait = 50;// Time to wait

		public BTReadThread(int wait) {
			this.wait = wait;
		}
		
		public void stopThread(){
			enRead = false;
		}

		public void run() {
			while (enRead) {
				try {
					if (BTConnectActivity.mBTSocket.getInputStream() != null) {
						byte[] tmp = new byte[1024];
						int len = BTConnectActivity.mBTSocket.getInputStream().read(tmp, 0, 1024); // 卡

						if (len > 0) {

							String res = "";
							for (int i = 0; i < tmp.length; i++) {
								String hex = Integer.toHexString(tmp[i] & 0xFF);
								if (hex.length() == 1) {
									hex = '0' + hex;
								}
								res += hex.toUpperCase();
							}

							String strDES = res.substring(0, 16);
							
							Message message = handler.obtainMessage();
							message.what = 0;
							message.obj = strDES;
							handler.sendMessage(message);
						}
					}
					Thread.sleep(wait);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}


记得添加权限:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

    
[2] popupWindow的应用
    来源: 互联网  发布时间: 2014-02-18
popupWindow的使用

1.定义一个Popupwindow:

private PopupWindow popWinOfKeyboard;
private LayoutInflater inflater ;

 

2.onCreate()

  inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.keyboard, null);
        
        popWinOfKeyboard = new PopupWindow(layout, 480, 350);//设置popupwindow的宽高
        popWinOfKeyboard.setAnimationStyle(R.style.PopupAnimation);
        popWinOfKeyboard.setOutsideTouchable(true);//设置点击PopopWindow的其他位置的时候关闭,无效!!
        
        View myPopView = popWinOfKeyboard.getContentView();//这个很重要!没有这个就找不到,就会nullpoint

                dialNum0 = (ImageButton)myPopView.findViewById(R.id.dial_num_0);
		dialNum1 = (ImageButton)myPopView.findViewById(R.id.dial_num_1);
		dialNum2 = (ImageButton)myPopView.findViewById(R.id.dial_num_2);
               //................
               dialNum0.setOnClickListener(this);
		dialNum1.setOnClickListener(this);
		dialNum2.setOnClickListener(this);
               //..............
 

3.显示

@Override
	public void onAttachedToWindow()
	{
		//放在这里面比较给力啊
		super.onAttachedToWindow();
		popWinOfKeyboard.showAtLocation(tvTitleText, Gravity.CENTER, 0, 145);//这里的布局可能有问题!!!
	}

 

4.关闭

popWinOfKeyboard.dismiss();
 

 


    
[3] List报表
    来源: 互联网  发布时间: 2014-02-18
List表格
线上效果图:
[img]

[/img]
工程结构图:
[img]

[/img]

布局文件:
list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="30dip" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="5" > 

        <TableRow android:id="@+id/list_header_row" >
			<!-- 小竖线 -->
            <View  />

            <TextView
                android:id="@+id/list_header_gyh"
                android:layout_width="80dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/color_black"
                android:padding="2dip"
                android:text="钢印号" />
                
			<!-- 小竖线 -->
            <View  />

            <TextView
                android:id="@+id/list_header_hm"
                android:layout_width="80dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/color_black"
                android:padding="2dip"
                android:text="户名" />
                
			<!-- 小竖线 -->
            <View  />

            <TextView
                android:id="@+id/list_header_wz"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="@color/color_black"
                android:padding="2dip"
                android:text="位置" />
			<!-- 小竖线 -->
            <View  />
        </TableRow>
    </TableLayout>

</LinearLayout>


list_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableLayout
        android:id="@+id/stock_list_item_table_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="5" >

        <TableRow android:id="@+id/stock_list_row" >

            <View  />

            <TextView
                android:id="@+id/item_tv_gyh"
                android:layout_width="80dip"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <View  />

            <TextView
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:id="@+id/item_tv_hm"
                android:layout_width="80dip"
                android:text="TextView" />

            <View  />

            <TextView
                android:layout_width="160dip"
                android:layout_height="wrap_content"
                android:id="@+id/item_tv_wz"
                android:text="TextView" />

            <View  />
        </TableRow>
    </TableLayout>

</LinearLayout>


notwriteinfo.xml
<?xml version="1.0" encoding="utf-8"?>
	<!-- 未抄表数据信息 -->
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/linearLayout1"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical"
	>
	
	<!-- 屏幕宽度的横线 -->
	<View  />

 	<!-- 添加表头 -->
	<include layout="@layout/list_header"/>

	<!-- 屏幕宽度的横线 -->
    <View  /> 
    
	<ListView
		android:id="@+id/notwriteinfo_lv_info"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:scrollingCache="true"
        android:fastScrollEnabled="true"
        android:focusable="true"
		android:divider="@color/color_dark_grey"
        android:dividerHeight="1dip" />
	
	<!-- 屏幕宽度的横线 -->
	<View  />
	
</LinearLayout>    


color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
  	 <color name="color_dark_grey">#808080</color>

    <color name="color_black">#000000</color>

    <color name="color_green">#00FF00</color>

    <color name="color_red">#FF0000</color>

    <color name="color_white">#FFFFFF</color>
  
</resources>


style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    
   <!-- 正文区背景色--> 
   <style name="bgColor" >
      <item name="android:background">#ffffff</item>    
   </style> 
  	<style name="StyContent"> 
  		<item name="android:textSize">18px</item> 
   		<item name="android:textColor">#000000</item>   
   </style>
  <style name="StyTitle"> 
  		<item name="android:textSize">24dip</item> 
   		<item name="android:textColor">#000000</item>   
   </style>
    <style name="StyButton"> 
  		<item name="android:textSize">24dip</item> 
   		<item name="android:textColor">#303030</item>   
   </style>
   <!-- Define the list items style begin -->
    <style name="list_item_seperator_layout">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">1dip</item>
        <item name="android:background">@color/color_dark_grey</item>
    </style>
    <style name="list_item_cell_seperator_layout">
        <item name="android:layout_width">1dip</item>
        <item name="android:layout_height">40dip</item>
        <item name="android:background">@color/color_dark_grey</item>
    </style>
    <!-- Define the list items style end -->
        
</resources>


MainActivity
package com.zzl.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    
	List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notwriteinfo);
        insertInfo();
      //取得ListView对象
        ListView listview = (ListView) findViewById(R.id.notwriteinfo_lv_info);
	      //给Listview添加元素
        listview.setAdapter(new MyAdapter(getApplicationContext(),list));
        //设置Listview滑动时背景不变黑
        listview.setCacheColorHint(Color.TRANSPARENT);
        
        
    }
    //模拟添加数据
    public void insertInfo(){
    	for(int i=0;i<20;i++){
    		HashMap<String, String> map = new HashMap<String,String>();
    		map.put("number", "888888");
    		map.put("name", "爆菊王");
    		map.put("address", "中国河北衡水桃城");
    		list.add(map);
    	}
    }
    
    
    /**
     * @author lyd
     */
    public class MyAdapter extends BaseAdapter {

    	private LayoutInflater mInflater;
    	//数据
    	private List<HashMap<String,String>> list = null;
    	
    	/* 构造函数 */
    	public MyAdapter(Context context,List<HashMap<String, String>> list2){
    		mInflater = LayoutInflater.from(context);
    		this.list = list2;
    	}
    	
    	/* 列表项个数 */
    	public int getCount() {
    		return list.size();
    	}

    	/* 获得列表指定的项 */
    	public Object getItem(int position) {
    		return null;
    	}

    	/* 获得列表指定项的Id */
    	public long getItemId(int position) {
    		return 0;
    	}

    	/* 设置显示格式 */
    	public View getView(int position, View convertView, ViewGroup parent) {

    		ViewHolder holder = null;

    		if (convertView == null) {// 当convertView 为null的时候初始化convertView
    			holder = new ViewHolder();
    			convertView = mInflater.inflate(R.layout.list_items, null);
    			holder.gyh = (TextView) convertView.findViewById(R.id.item_tv_gyh);
    			holder.hm = (TextView) convertView.findViewById(R.id.item_tv_hm);
    			holder.wz = (TextView) convertView.findViewById(R.id.item_tv_wz);
    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
    		holder.gyh.setText(list.get(position).get("number"));
    		holder.hm.setText(list.get(position).get("name"));
    		holder.wz.setText(list.get(position).get("address"));
    		holder.gyh.setTextColor(Color.BLACK);
    		holder.hm.setTextColor(Color.BLACK);
    		holder.wz.setTextColor(Color.BLACK);
    		
    		return convertView;
    	}

    	/* 自定义实体类,属性与List项对应 */
    	public final class ViewHolder {
    		private int id;
    		public TextView gyh;// 钢印号


    		public TextView hm;// 户名
    		public TextView wz;// 位置
    		public TextView dj;// 电价
    		public int getId() {
    			return id;
    		}
    		public void setId(int id) {
    			this.id = id;
    		}
    		public TextView getGyh() {
    			return gyh;
    		}
    		public void setGyh(TextView gyh) {
    			this.gyh = gyh;
    		}
    		public TextView getHm() {
    			return hm;
    		}
    		public void setHm(TextView hm) {
    			this.hm = hm;
    		}
    		public TextView getWz() {
    			return wz;
    		}
    		public void setWz(TextView wz) {
    			this.wz = wz;
    		}
    		public TextView getDj() {
    			return dj;
    		}
    		public void setDj(TextView dj) {
    			this.dj = dj;
    		}
    	}
    }
    
}

    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3