我已經開發了一個android
應用程序到rc汽車在wifi上。我已經使用了esp8266 nodemcu
wifi模塊用於此目的。在我的應用程序。有4個按鈕,分別是前進,後退,左右。我可以通過wifi連接這個模塊,併爲ex做一些基本的操作。前進,後退,右轉,左轉。下面是執行此操作的代碼:如何在Android中同時處理兩個按鈕同時按下事件
@Override
public boolean onTouch(View v, MotionEvent event) {
String action;
if (v.getId() == moveForward.getId())
action = "/forward/";
else if (v.getId() == moveReverse.getId())
action = "/reverse/";
else if (v.getId() == moveRigth.getId())
action = "/right/";
else
action = "/left/";
String serverAddress = ipAddress.getText().toString() + ":" + "80" + action;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
HttpRequestTask requestTask = new HttpRequestTask(serverAddress);
requestTask.execute("1");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
HttpRequestTask requestTask = new HttpRequestTask(serverAddress);
requestTask.execute("0");
}
return false;
}
private class HttpRequestTask extends AsyncTask<String, Void, String> {
private String serverAdress;
private String serverResponse = "";
public HttpRequestTask(String serverAdress) {
this.serverAdress = serverAdress;
}
@Override
protected String doInBackground(String... params) {
String val = params[0];
String url = "http://" + serverAdress + val;
Log.e("url", url);
try {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet();
getRequest.setURI(new URI(url));
HttpResponse response = client.execute(getRequest);
InputStream inputStream = null;
inputStream = response.getEntity().getContent();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
serverResponse = bufferedReader.readLine();
inputStream.close();
} catch (URISyntaxException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (ClientProtocolException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
serverResponse = e.getMessage();
}
return serverResponse;
}
當用戶按下向前也可以按右鍵或左鍵並車子前進,右轉或反向移動,並在同一時間左轉。但我不知道如何在代碼中同時處理兩個按鈕按下的事件。你能告訴我一個方法嗎?
沒有你的屏幕支持多點觸控? –
@OussemaAroua我不知道。如果是,如何處理? – user3059066
只需在您的內容佈局中添加android:splitMotionEvents =「true」 –