在这里,我通过蓝牙连接笔记本电脑和Android。
我的问题是,当我不按下按钮时如何设置数据。
我的android应用程序发送+, - 当我按下+按钮和 - 按钮时。
如果我按下+按钮,笔记本电脑中的程序会增加值,而 - 按钮会产生相反的结果。
但我需要的是逐个改变价值。
这意味着,只需单击一次即可增加该值,而不是持续增加该值。
下面的代码发送当我不按下按钮时推送到最后的数据。
package remote.bluetooth;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MouseActivity extends Activity implements OnClickListener{
int score = 0;
TextView Value;
Button Plus, Minus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mouseactivity);
Plus = (Button) findViewById(R.id.RClick);
Minus = (Button) findViewById(R.id.LClick);
Value = (TextView) findViewById(R.id.number);
Plus.setOnClickListener((OnClickListener) this);
Minus.setOnClickListener((OnClickListener) this);
//왼 클릭 버튼 생성 및 이벤트 연결
}
public void onClick(View v)
{
if(v.getId()==R.id.RClick)
{
String msg = "+";
Main.getInstance().sendMessage(msg);
score++;
Value.setText(String.valueOf(score));
}
else if (v.getId()==R.id.LClick)
{
String msg = "-";
Main.getInstance().sendMessage(msg);
score--;
Value.setText(String.valueOf(score));
}
else
{
String msg = "0";
Main.getInstance().sendMessage(msg);
}}
}
和发送消息方法
public void sendMessage(String mMsg) {
// Check that we're actually connected before trying anything
if (mConnection.getState() != BluetoothConnection.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
// Check that there's actually something to send
if (mMsg.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = mMsg.getBytes();
mConnection.write(send);
}
}
答案 0 :(得分:0)
您正在递增和递减score
变量两次。
下面:
case R.id.RClick: score++; showText = true; break;
在这里:
if(v.getId()==R.id.RClick){
String msg = "+";
Main.getInstance().sendMessage(msg);
score++;
Value.setText(String.valueOf(score));
}