我使用 onDragListener 在屏幕上拖动一个对象,这部分效果很好。但我还需要在拖动时检查屏幕的 x,y 坐标。我重写了 onTouch 方法,它也运行良好。但是一旦我拖动对象,ontouch 侦听器就不起作用。我无法让两个听众一起工作。我不明白为什么它不能一起工作。
以下代码用于拖动
ima.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),
mimeTypes, item);
// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
); …Run Code Online (Sandbox Code Playgroud) 我现在正在一个项目中工作,我希望使用gps获取用户的当前位置.我已经阅读了很多教程并尝试编写代码.该代码在模拟器中运行良好.每次我使用DDMS发送latittude和经度时,模拟器都会检测到它.主要问题是它不适用于真实设备.虽然,我尝试的设备有互联网接入,GPS是开放的.我已将所需权限添加到清单文件中.
public class MainActivity extends Activity {
TextView lat;
TextView lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat=(TextView)findViewById(R.id.textView2);
lon=(TextView)findViewById(R.id.textView4);
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll=new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class myLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location!=null){
double pLat=location.getLatitude();
double pLong=location.getLongitude();
lat.setText(Double.toString(pLat));
lon.setText(Double.toString(pLong));
}
}
@Override …Run Code Online (Sandbox Code Playgroud)