当前位置:  编程技术>移动开发
本页文章导读:
    ▪怎么在按上(但是没有长按)和长按时分别执行不同的操作        如何在按下(但是没有长按)和长按时分别执行不同的操作由于onDown在长按时也会执行,所以不能再onDown和长按监听器的onlongclick中分别执行各种的动作(这样长按时会执行onlongclick和ondown两.........
    ▪ andriod 自动切换网络跟gps定位        andriod 自动切换网络和gps定位 获取到位置服务以后,同时请求网络和gps定位更新,然后就会同时上报网络和gps的Location 信息。在没有gps信号的时候,会自动获取网络定位的位置信息,如果有gp.........
    ▪ 应用ViewGroup自定义数字键盘       使用ViewGroup自定义数字键盘首先看效果图 1.继承viewGroup实现GirdDialpad控件 package com.android.dialpad; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.........

[1]怎么在按上(但是没有长按)和长按时分别执行不同的操作
    来源: 互联网  发布时间: 2014-02-18
如何在按下(但是没有长按)和长按时分别执行不同的操作

由于onDown在长按时也会执行,所以不能再onDown和长按监听器的onlongclick中分别执行各种的动作(这样长按时会执行onlongclick和ondown两个动作)

使用手势可以很好解决这个问题

package lon.detector;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;

public class MyText extends TextView implements OnGestureListener
{
    private GestureDetector detector;
    
    public MyText(Context context)
    {
        super(context);
        detector = new GestureDetector(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        detector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    public boolean onDown(MotionEvent e)
    {
        Log.e("-----", "---: on Down");
        return false;
    }

    public void onShowPress(MotionEvent e) 
    {
        Log.e("-----", "---: on ShowPress");
        
    }

    public boolean onSingleTapUp(MotionEvent e)
    {
        Log.e("-----", "---: on SingleTapUp");
        return false;
    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) 
    {
        Log.e("-----", "---: on Scroll");
        return false;
    }

    public void onLongPress(MotionEvent e) 
    {
        Log.e("-----", "---: on Long");
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) 
    {
        Log.e("-----", "---: on fling");
        return false;
    }

}
onLongPress里写长按事件

onSingleTapUp里写非长按的点击事件
这样就不会出现上述长按会调用两个动作的问题了

1楼wsz1z154昨天 21:12您所说的按下(非长按)和长按n为什么不能用onClick和onlongclickn来区分呢?

    
[2] andriod 自动切换网络跟gps定位
    来源: 互联网  发布时间: 2014-02-18
andriod 自动切换网络和gps定位

获取到位置服务以后,同时请求网络和gps定位更新,然后就会同时上报网络和gps的Location 信息。在没有gps信号的时候,会自动获取网络定位的位置信息,如果有gps信号,则优先获取gps提供的位置信息.isBetterLocation 根据 时间、准确性、定位方式等判断是否更新当前位置信息,该方法来源于开发指南的Obtaining User Location 下。 


package cncit.gps;


import java.text.SimpleDateFormat;
import java.util.Date;


import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class UploadgpsActivity extends Activity
{
LocationManager lm = null;
Location myLocation = null;
TextView loc, timeText;


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss.SSSZ");


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


loc = (TextView) findViewById(R.id.loc);
timeText = (TextView) findViewById(R.id.time);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}


@Override
protected void onResume()
{
super.onResume();


lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
listener);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
listener);


Log.e("onResume", "onResume");
}


@Override
protected void onPause()
{
super.onPause();


Log.e("onPause", "onPause");


lm.removeUpdates(listener);
}


 


LocationListener listener = new LocationListener()
{


@Override
public void onLocationChanged(Location location)
{
// 实际上报时间
//String time = sdf.format(new Date(location.getTime()));
//timeText.setText("实际上报时间:" + time);
 
if (isBetterLocation(location, myLocation))
{
//获取纬度
double lat = location.getLatitude();
//获取经度
double lon = location.getLongitude();
                                //位置提供者
String provider = location.getProvider();
                                //位置的准确性
float accuracy = location.getAccuracy();
//高度信息
double altitude = location.getAltitude();
//方向角
float bearing = location.getBearing();
//速度 米/秒
float speed = location.getSpeed();

String locationTime = sdf.format(new Date(location.getTime()));
String currentTime = null;
 
if (myLocation != null)

currentTime = sdf.format(new Date(myLocation.getTime()));
myLocation =location; 

}
else 
{
myLocation =location; 


loc.setText("经度:" + lon 
+ "\n纬度:" + lat 
+ "\n服务商:"+ provider
+ "\n准确性:"+ accuracy
+ "\n高度:"+ altitude
+ "\n方向角:"+ bearing
+ "\n速度:"+ speed 
+ "\n上次上报时间:"+currentTime
+ "\n最新上报时间:"+locationTime);  


}





}


@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e("onStatusChanged", "onStatusChanged: " + provider);


}


@Override
public void onProviderEnabled(String provider)
{
Log.e("onProviderEnabled", "onProviderEnabled: " + provider);
}


@Override
public void onProviderDisabled(String provider)
{
Log.e("onProviderDisabled", "onProviderDisabled: " + provider);
}


};


private static final int TWO_MINUTES = 1000 * 1 * 2;


/**
* Determines whether one Location reading is better than the current
* Location fix

* @param location
*            The new Location that you want to evaluate
* @param currentBestLocation
*            The current Location fix, to which you want to compare the new
*            one
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation)
{
if (currentBestLocation == null)
{
// A new location is always better than no location
return true;
}


// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;


// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer)
{
return true;
// If the new location is more than two minutes older, it must be
// worse
}
else if (isSignificantlyOlder)
{
return false;
}


// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;


// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());


// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate)
{
return true;
}
else if (isNewer && !isLessAccurate)
{
return true;
}
else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)
{
return true;
}
return false;
}


/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2)
{
if (provider1 == null)
{
return provider2 == null;
}
return provider1.equals(provider2);
}


}

    
[3] 应用ViewGroup自定义数字键盘
    来源: 互联网  发布时间: 2014-02-18
使用ViewGroup自定义数字键盘

首先看效果图

1.继承viewGroup实现GirdDialpad控件

package com.android.dialpad;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class GridDialPad extends ViewGroup{
	int row=0;
	int colum=0;
	private int mHeight;

	public GridDialPad(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	public GridDialPad(Context context ,AttributeSet atts){
		super(context, atts);
	}
	public GridDialPad(Context context,AttributeSet attrs, int defstyle){
		super(context, attrs, defstyle);
		TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GridDialPad);
		row=a.getInt(R.styleable.GridDialPad_row, 3);
		colum=a.getInt(R.styleable.GridDialPad_colum,3);
		Log.i("debug", "print"+"row="+row+",colum="+colum);
		a.recycle();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		int index=0;
		int y=(b-t)-mHeight+getPaddingTop();
		for (int i = 0; i < row; i++) {
			int x=getPaddingLeft();
			int btnHeight=getChildAt(index).getMeasuredHeight();
			for (int j = 0; j < colum; j++) {
				View child=getChildAt(index);
				int btnWidth=child.getMeasuredWidth();
				child.layout(x, y, x+btnWidth, y+btnHeight);
				x+=btnWidth;
				index++;
			}
			y+=btnHeight;
			
		}
		
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int totalwidth=0;
		int totalheight=0;
		int index=0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < colum; j++) {
			 View view=getChildAt(index);
			 view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
				if(i==0){
					totalwidth+=view.getMeasuredHeight();
				}
				i++;
			}
			totalheight=getChildAt(index).getMeasuredHeight();
		}
		
		int width=resolveSize(totalwidth, widthMeasureSpec);
		int height=resolveSize(totalheight, heightMeasureSpec);
		mHeight=height;
		setMeasuredDimension(width, height);
	}

}

2..layout布局中使用用自定义的GridDialPad

 


 

<?xml version="1.0" encoding="utf-8"?>
<com.android.dialpad.GridDialPad
    xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:dial="http://schemas.android.com/apk/res/com.android.dialpad"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    dial:row="3"
    dial:colum="3"
     >
   
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          
            android:background="@drawable/dial_1" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dial_2" 
              />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
          
            android:background="@drawable/dial_3" />
     
 


        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           
            android:background="@drawable/dial_4" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dial_5"
            />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dial_6"
            />


   

        <ImageButton
            android:id="@+id/imageButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dial_7"
             />

        <ImageButton
            android:id="@+id/imageButton9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dial_8"
             />

        <ImageButton
            android:id="@+id/imageButton8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dial_9" 
            />


</com.android.dialpad.GridDialPad >


3.界面布局文件mian.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
       <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="26dp"
        android:layout_marginTop="38dp"
        android:ems="10">

    </EditText>
    <include layout="@layout/dialpad"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        
        android:layout_height="wrap_content"
        />

 

 </RelativeLayout>

 4.Activity实现

package com.android.dialpad;


import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class NumberDialPadActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	EditText text;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text=(EditText)findViewById(R.id.editText1);
        setupDial();
    }
    private void  setupDial(){
    	findViewById(R.id.imageButton1).setOnClickListener(this);
    	findViewById(R.id.imageButton2).setOnClickListener(this);
    	findViewById(R.id.imageButton3).setOnClickListener(this);
    	findViewById(R.id.imageButton4).setOnClickListener(this);
    	findViewById(R.id.imageButton5).setOnClickListener(this);
    	findViewById(R.id.imageButton6).setOnClickListener(this);
    	
    	findViewById(R.id.imageButton7).setOnClickListener(this);
    	findViewById(R.id.imageButton8).setOnClickListener(this);
    	findViewById(R.id.imageButton9).setOnClickListener(this);
    	
    }
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.imageButton1:
			keyPressed(KeyEvent.KEYCODE_1);
			break;
case R.id.imageButton2:
	keyPressed(KeyEvent.KEYCODE_2);
			break;
case R.id.imageButton3:
	keyPressed(KeyEvent.KEYCODE_3);
	break;
case R.id.imageButton4:
	keyPressed(KeyEvent.KEYCODE_4);
	break;
case R.id.imageButton5:
	keyPressed(KeyEvent.KEYCODE_5);
	break;
	
case R.id.imageButton6:
	keyPressed(KeyEvent.KEYCODE_6);
	break;
case R.id.imageButton7:
	keyPressed(KeyEvent.KEYCODE_7);
	break;
case R.id.imageButton8:
	keyPressed(KeyEvent.KEYCODE_8);
	break;
case R.id.imageButton9:
	keyPressed(KeyEvent.KEYCODE_9);
	break;
		default:
			break;
		}
		
	}
	
	private void keyPressed(int keyCode){
		KeyEvent event=new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
		text.onKeyDown(keyCode, event);
		
	}
}

其它文件


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 自定属性 -->
  <declare-styleable name="GridDialPad">  
     <attr name="row" format="integer" />   
      <attr name="colum" format="integer" />   
  </declare-styleable>
    
</resources>


 代码下载http://download.csdn.net/detail/androidchuxueze/4467439

 


    
最新技术文章:
▪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