0
我有三个选择从微调,如果我选择(例如:“选择模式”),然后点击下一个按钮来发送我选择的布局, 等等,我想使用开关案件,但我不知道如何做到这一点嗨!我怎么能进入我从微调选择的布局?
public class ParkingModeActivity extends Activity {
private Spinner spinner1;
private Button btnNext;
private Button btnLedControl;
private int temp = 0;
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private String address = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parking_mode);
btnNext = (Button) findViewById(R.id.btn_next);
Intent intent = getIntent();
this.address = intent.getStringExtra("pairedMac");
addListenerOnButton();
addListenerOnSpinnerItemSelection();
btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
btnLedControl = (Button) findViewById(R.id.btn_ledControl);
btnLedControl.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (temp){
case 0:
sendData("1");
Toast msg = Toast.makeText(getBaseContext(),
"LED IS ON", Toast.LENGTH_SHORT);
msg.show();
temp++;
break;
case 1:
sendData("0");
Toast msg2 = Toast.makeText(getBaseContext(),
"LED IS OFF", Toast.LENGTH_SHORT);
msg2.show();
temp--;
break;
}
}
});
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
}
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnNext = (Button) findViewById(R.id.btn_next);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (temp) {
case 0:
if (btnNext.equals(spinner1) {
Intent intent = new Intent(ParkingModeActivity.this, ChooseMode.class);
startActivity(intent);
}
}
Toast.makeText(ParkingModeActivity.this,
"OnClickListener : " +
"\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResume() {
super.onResume();
Log.d("SEND BT SERIAL", "In onResume - Attempting client connect");
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
btAdapter.cancelDiscovery();
Log.d("SEND BT SERIAL", "Connecting to Remote");
try {
btSocket.connect();
Log.d("SEND BT SERIAL", "Connection established and data link opened...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
Log.d("SEND BT SERIAL", "Creating Socket");
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}
@Override
public void onPause() {
super.onPause();
Log.d("SEND BT SERIAL", "In onPause()");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ". Unplug bluetooth module and then plug back.");
}
}
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
private void checkBTState() {
if(btAdapter==null) {
errorExit("Fatal Error", "Bluetooth Not supported. Aborting.");
} else {
if (btAdapter.isEnabled()) {
Log.d("SEND BT SERIAL", "Bluetooth is enabled");
} else {
Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
private void errorExit(String title, String message){
Toast msg = Toast.makeText(getBaseContext(),
title + " - " + message, Toast.LENGTH_LONG);
msg.show();
finish();
}
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
Log.d("SEND BT SERIAL", "Sending data: " + message + "");
try {
outStream.write(msgBuffer);
} catch (IOException e) {
String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
if (address.equals("00:00:00:00:00:00"))
msg = msg + ".\n\nUpdate your server address.";
msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
//errorExit("Fatal Error", msg);
}
}
}
//and my layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_dark">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="@android:color/background_dark">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@drawable/contilogo"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/parking_mode_description"
android:gravity="center"
android:textColor="#FFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/mode_prompt"
android:textColor="#FFF"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/parking_options"
android:prompt="@string/mode_prompt"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"/>
<Button
android:id="@+id/btn_ledControl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LED CONTROL"
android:textColor="#faa61a"
android:background="@android:color/background_dark"/>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/text_next"
android:textColor="#faa61a"
android:background="@android:color/background_dark" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
你的确切问题是什么?你喜欢那个'switch'?你只是在这里暴露了近300行代码... – shkschneider