我正在开发基于BluetoothChat示例的应用程序,其中我有2个按钮的主要活动:
1. Find devices
2. Configuration
使用第一个按钮,我搜索的代码与上述示例中的代码相同。
使用第二个按钮,我进入另一个活动,其中我有3个搜索栏,每个搜索栏下面都有文本视图,显示当前搜索栏的值。
我需要的是修改BluetoothChat示例代码以发送具有这3个值而不是文本的数组。
这是我正在尝试开发此活动的代码。
我该怎么做?
/*********************
*
* ONCREATE
*
********************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
bar1 = (SeekBar)findViewById(R.id.fastbar);
bar1.setOnSeekBarChangeListener(this);
progress1 = (TextView)findViewById(R.id.out1);
bar2 = (SeekBar)findViewById(R.id.climbbar);
bar2.setOnSeekBarChangeListener(this);
progress2 = (TextView)findViewById(R.id.out2);
bar3 = (SeekBar)findViewById(R.id.platformbar);
bar3.setOnSeekBarChangeListener(this);
progress3 = (TextView)findViewById(R.id.out3);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
/**Initialize the buffer for outgoing messages*/
GlobalVar.mOutStringBuffer = new StringBuffer("");
}
/*********************
*
* SEEKBAR
*
********************/
/**Method to get each seekbar's value and save it into a textview*/
@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
switch (bar.getId()) {
case R.id.fastbar:
progress1.setText(progress + "%");
break;
case R.id.climbbar:
progress2.setText(progress + "%");
break;
case R.id.platformbar:
progress3.setText(progress + "%");
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
}
/*********************
*
* ONRESUME
*
********************/
/**Load seekbar's values from sharedpreferences*/
@Override
protected void onResume() {
super.onResume();
loadedProgress1 = mPreferences.getInt(Key_PROGRESS_1, 50);
bar1.setProgress(loadedProgress1);
loadedProgress2 = mPreferences.getInt(Key_PROGRESS_2, 90);
bar2.setProgress(loadedProgress2);
loadedProgress3 = mPreferences.getInt(Key_PROGRESS_3, 70);
bar3.setProgress(loadedProgress3);
}
/*********************
*
* ONPAUSE
*
********************/
/**Save seekbar's values on sharedfreferences*/
@Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = mPreferences.edit();
savedProgress1 = bar1.getProgress();
editor.putInt(Key_PROGRESS_1, savedProgress1);
savedProgress2 = bar2.getProgress();
editor.putInt(Key_PROGRESS_2, savedProgress2);
savedProgress3 = bar3.getProgress();
editor.putInt(Key_PROGRESS_3, savedProgress3);
editor.commit();
}
/*********************
*
* OPTIONS
*
********************/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/**Inflate the menu; this adds items to the action bar if it is present.*/
getMenuInflater().inflate(R.menu.menu_conf, menu);
return true;
}
/**Options menu where I call the send function*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send:
sendValues();
break;
case R.id.reset:
resetValues(null);
break;
}
return true;
}
/*********************
*
* SEND & RESET
*
********************/
public void sendValues() {
/**Seekbars values*/
String message1 = progress1.getText().toString();
String message2 = progress2.getText().toString();
String message3 = progress3.getText().toString();
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**
*
*HERE IS WHERE I'M TRYING TO CREATE A FUNCTION TO SEND THE 3 VALUES IN A STRING TYPE MESSAGE
*THIS CODE IS FROM THE SETUPCHAT() FUNCTION, SO IS WRONG IN THIS CASE
*/
/**Get the message bytes and tell the to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}
public void resetValues() {
defaultValue1 = 50;
bar1.setProgress(defaultValue1);
defaultValue2 = 90;
bar2.setProgress(defaultValue1);
defaultValue3 = 70;
bar3.setProgress(defaultValue1);
}
这是创建的新功能:
public void sendValues() {
/**Send the seekbars values*/
String message = progress1.getText().toString()+":"+progress2.getText().toString()+":"+progress3.getText().toString();
String[] values = message.split(":");
for (String value : values) {
int number = Integer.valueOf(value);
}
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**Get the message bytes and tell the Transmission to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}
答案 0 :(得分:0)
您可以使用regularExpression ex将字符串作为字符串发送。 "5:15:33"
,然后在":"
上收到字符串时将其拆分。
String str = bar1.getText().toString()+":"+bar2.getText().toString()+":"+bar3.getText().toString();
将str String发送到reciver,然后你可以像这样从String数组中获取int
String[] values = str.split(":");
for (String value : values) {
int number = Integer.valueOf(value);
}