问题描述
我正在开发我的第一个Android应用程序,以独立控制20个RGB LED,目前我仅在基本的GUI结构上工作。 我的第一个窗格几乎可以完美工作(setPressed方法没有实现我希望的并且该类尚未进行优化),但是我的第二个窗格没有显示我创建的对象。 这是我的第一窗格和第二窗格的代码:
activity_controller.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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=".Controller"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/button0"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button0"
android:onClick="toggleButton0"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button1"
android:onClick="toggleButton1"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button2"
android:onClick="toggleButton2"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button3"
android:onClick="toggleButton3"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button4"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button4"
android:onClick="toggleButton4"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button5"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button5"
android:onClick="toggleButton5"
style="?android:attr/buttonStyleToggle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/row2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/row1"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/button6"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button6"
android:onClick="toggleButton6"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/buttonAll"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/buttonAll"
android:onClick="selectAll"/>
<Button
android:id="@+id/button7"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button7"
android:onClick="toggleButton7"
style="?android:attr/buttonStyleToggle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/row3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/row2"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/button8"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button8"
android:onClick="toggleButton8"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/buttonSet"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/buttonSet"
android:onClick="setColor"/>
<Button
android:id="@+id/button9"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button9"
android:onClick="toggleButton9"
style="?android:attr/buttonStyleToggle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/row4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/row3"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/button10"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button10"
android:onClick="toggleButton10"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/buttonOff"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/buttonOff"
android:onClick="turnOff"/>
<Button
android:id="@+id/button11"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button11"
android:onClick="toggleButton11"
style="?android:attr/buttonStyleToggle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/row5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/row4"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/button12"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button12"
android:onClick="toggleButton12"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/buttonBuiltIn"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/buttonBuiltIn"
android:onClick="builtIns"/>
<Button
android:id="@+id/button13"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button13"
android:onClick="toggleButton13"
style="?android:attr/buttonStyleToggle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/row6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/row5"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/button14"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button14"
android:onClick="toggleButton14"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button15"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button15"
android:onClick="toggleButton15"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button16"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button16"
android:onClick="toggleButton16"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button17"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button17"
android:onClick="toggleButton17"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button18"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button18"
android:onClick="toggleButton18"
style="?android:attr/buttonStyleToggle"/>
<Button
android:id="@+id/button19"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/button19"
android:onClick="toggleButton19"
style="?android:attr/buttonStyleToggle"/>
</LinearLayout>
</RelativeLayout>
Controller.java:
package com.example.lightcontrol;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class Controller extends Activity {
public final static String SELECTED_BUTTONS = "com.mycompany.myfirstapp.BUTTONS";
public final static boolean[] selectedButtons = new boolean[20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controller);
int i = 0;
while (i < 20) {
selectedButtons[i]=false;
i++;
}
}
@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_controller, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void toggleButton0(View view) {
if (!selectedButtons[0]) {
view.setPressed(true);
selectedButtons[0] = true;
} else {
view.setPressed(false);
selectedButtons[0] = false;
}
}
public void toggleButton1(View view) {
if (!selectedButtons[1]) {
view.setPressed(true);
selectedButtons[1] = true;
} else {
view.setPressed(false);
selectedButtons[1] = false;
}
}
public void toggleButton2(View view) {
if (!selectedButtons[2]) {
view.setPressed(true);
selectedButtons[2] = true;
} else {
view.setPressed(false);
selectedButtons[2] = false;
}
}
public void toggleButton3(View view) {
if (!selectedButtons[3]) {
view.setPressed(true);
selectedButtons[3] = true;
} else {
view.setPressed(false);
selectedButtons[3] = false;
}
}
public void toggleButton4(View view) {
if (!selectedButtons[4]) {
view.setPressed(true);
selectedButtons[4] = true;
} else {
view.setPressed(false);
selectedButtons[4] = false;
}
}
public void toggleButton5(View view) {
if (!selectedButtons[5]) {
view.setPressed(true);
selectedButtons[5] = true;
} else {
view.setPressed(false);
selectedButtons[5] = false;
}
}
public void toggleButton6(View view) {
if (!selectedButtons[6]) {
view.setPressed(true);
selectedButtons[6] = true;
} else {
view.setPressed(false);
selectedButtons[6] = false;
}
}
public void toggleButton7(View view) {
if (!selectedButtons[7]) {
view.setPressed(true);
selectedButtons[7] = true;
} else {
view.setPressed(false);
selectedButtons[7] = false;
}
}
public void toggleButton8(View view) {
if (!selectedButtons[8]) {
view.setPressed(true);
selectedButtons[8] = true;
} else {
view.setPressed(false);
selectedButtons[8] = false;
}
}
public void toggleButton9(View view) {
if (!selectedButtons[9]) {
view.setPressed(true);
selectedButtons[9] = true;
} else {
view.setPressed(false);
selectedButtons[9] = false;
}
}
public void toggleButton10(View view) {
if (!selectedButtons[10]) {
view.setPressed(true);
selectedButtons[10] = true;
} else {
view.setPressed(false);
selectedButtons[10] = false;
}
}
public void toggleButton11(View view) {
if (!selectedButtons[11]) {
view.setPressed(true);
selectedButtons[11] = true;
} else {
view.setPressed(false);
selectedButtons[11] = false;
}
}
public void toggleButton12(View view) {
if (!selectedButtons[12]) {
view.setPressed(true);
selectedButtons[12] = true;
} else {
view.setPressed(false);
selectedButtons[12] = false;
}
}
public void toggleButton13(View view) {
if (!selectedButtons[13]) {
view.setPressed(true);
selectedButtons[13] = true;
} else {
view.setPressed(false);
selectedButtons[13] = false;
}
}
public void toggleButton14(View view) {
if (!selectedButtons[14]) {
view.setPressed(true);
selectedButtons[14] = true;
} else {
view.setPressed(false);
selectedButtons[14] = false;
}
}
public void toggleButton15(View view) {
if (!selectedButtons[15]) {
view.setPressed(true);
selectedButtons[15] = true;
} else {
view.setPressed(false);
selectedButtons[15] = false;
}
}
public void toggleButton16(View view) {
if (!selectedButtons[16]) {
view.setPressed(true);
selectedButtons[16] = true;
} else {
view.setPressed(false);
selectedButtons[16] = false;
}
}
public void toggleButton17(View view) {
if (!selectedButtons[17]) {
view.setPressed(true);
selectedButtons[17] = true;
} else {
view.setPressed(false);
selectedButtons[17] = false;
}
}
public void toggleButton18(View view) {
if (!selectedButtons[18]) {
view.setPressed(true);
selectedButtons[18] = true;
} else {
view.setPressed(false);
selectedButtons[18] = false;
}
}
public void toggleButton19(View view) {
if (!selectedButtons[19]) {
view.setPressed(true);
selectedButtons[19] = true;
} else {
view.setPressed(false);
selectedButtons[19] = false;
}
}
/** Called when the user clicks the Send button */
public void setColor(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(SELECTED_BUTTONS, selectedButtons);
startActivity(intent);
}
}
activity_display_message.xml:
<RelativeLayout
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: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=".DisplayMessageActivity"
android:orientation="horizontal">
<SeekBar
android:id="@+id/redSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:max="255"/>
<SeekBar
android:id="@+id/greenSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_below="@+id/redSlider"
android:max="255"/>
<SeekBar
android:id="@+id/blueSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_below="@+id/greenSlider"
android:max="255"/>
<TextView
android:id="@+id/colorBox"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
DisplayMessageActivity.java:
package com.example.lightcontrol;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.ArrayList;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
boolean[] buttons = intent.getBooleanArrayExtra(Controller.SELECTED_BUTTONS);
// Create the text view
String message = "";
int i = 0;
for(boolean currButton: buttons) {
if(currButton) {
message += Integer.toString(i) + ",";
}
i++;
}
final SeekBar redSlider = (SeekBar) findViewById(R.id.redSlider);
final SeekBar blueSlider = (SeekBar) findViewById(R.id.blueSlider);
final SeekBar greenSlider = (SeekBar) findViewById(R.id.greenSlider);
ArrayList<SeekBar> seekArray = new ArrayList<SeekBar>();
seekArray.add(redSlider);
seekArray.add(blueSlider);
seekArray.add(greenSlider);
for (SeekBar slider : seekArray) {
slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar slider, int progresValue, boolean fromUser) {
updateColorBox();
}
@Override
public void onStartTrackingTouch(SeekBar slider) {
}
@Override
public void onStopTrackingTouch(SeekBar slider) {
}
});
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void updateColorBox() {
SeekBar redSlider = (SeekBar) findViewById(R.id.redSlider);
SeekBar blueSlider = (SeekBar) findViewById(R.id.blueSlider);
SeekBar greenSlider = (SeekBar) findViewById(R.id.greenSlider);
TextView colorBox = (TextView) findViewById(R.id.colorBox);
int redProgress = redSlider.getProgress();
int blueProgress = blueSlider.getProgress();
int greenProgress = greenSlider.getProgress();
String hex = String.format("%02x%02x%02x", redProgress, greenProgress, blueProgress);
colorBox.setBackgroundColor(Color.parseColor(hex));
}
}
有谁知道为什么DisplayMessageActivity窗格为空白? 在我的xml设计预览标签中,它不是空白...谢谢!
1楼
您缺少对onCreate()
setContentView()
的调用