Я создаю кучу кнопок плюс и минус для приложения сбора данных. Я использую конечный автомат.
Когда я нажимаю кнопки в моем приложении, TextView
, который отображает число, должно измениться, но это не так. Что я сделал не так?
Это соответствующий код:
package com.example.scoutingapp3250;
import android.content.Intent;
import android.icu.lang.UCharacter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import
com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.zxing.client.android.Intents;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static String finalOutput;
public static StringBuilder stringBuilder;
public enum ScanState {
scanning,
empty,
stopped
}
ScanState scanState;
@Override
protected void onCreate(Bundle savedInstanceState) {
stringBuilder = new StringBuilder();
super.onCreate(savedInstanceState);
super.onResume();
findViewById(R.id.minusButtonAutoPowerPortBottom).setOnClickListener(this);
findViewById(R.id.plusButtonAutoPowerPortBottom).setOnClickListener(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button submit = findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkForms();
//if (checkForms();return;void)
openActivity2();
}
});
}
public void checkForms() {
scanState = ScanState.scanning;
ScrollView scrollView = findViewById(R.id.scroll);
LinearLayout linearLayout = findViewById(R.id.linearLayout);
final int childCount = linearLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View element = linearLayout.getChildAt(i);
switch (scanState) {
case scanning:
if (element instanceof EditText) {
EditText editText = (EditText) element;
if (editText.getText().toString().matches("")) {
scrollView.scrollTo(0, editText.getTop() + 10);
scanState = ScanState.empty;
break;
}
break;
}
if (element instanceof RadioGroup) {
RadioGroup radioGroup = (RadioGroup) element;
RadioButton radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
if (radioGroup.getCheckedRadioButtonId() == -1) {
scrollView.scrollTo(0, radioGroup.getTop());
scanState = ScanState.empty;
break;
}
}
scanState = ScanState.stopped;
break;
case empty:
break;
case stopped:
break;
}
}
}
public void openActivity2() {
output();
Intent intent = new Intent(MainActivity.this, QrCodeGenerator.class);
intent.putExtra("data", stringBuilder.toString());
startActivity(intent);
}
public void openPitScout() {
Intent pitScout = new Intent (MainActivity.this, PitScouter.class);
startActivity(pitScout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, 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.pitScoutSheet) {
openPitScout();
return true;
}
return super.onOptionsItemSelected(item);
}
private void addInt(int stateID) {
TextView tView = findViewById(stateID);
int value = Integer.parseInt(tView.getText().toString());
value += 1;
String temp = Integer.toString(value); tView.setText(temp);
//tView.setText(Integer.toString(value));
}
private void subInt(int stateID) {
TextView tView = findViewById(stateID);
int value = Integer.parseInt(tView.getText().toString());
value -= 1;
String temp = Integer.toString(value); tView.setText(temp);
//tView.setText(Integer.toString(value));
}
public void output () {
EditText scoutName = findViewById(R.id.editSN);
TextView teamNumber = findViewById(R.id.editTN);
EditText matchNumber = findViewById(R.id.editMatchNumber);
RadioGroup alliance = findViewById(R.id.alliance);
RadioButton allianceI = findViewById(alliance.getCheckedRadioButtonId());
RadioGroup driverStation = findViewById(R.id.driverStation);
RadioButton driverStationI = findViewById(driverStation.getCheckedRadioButtonId());
RadioGroup crossLine = findViewById(R.id.crossLine);
RadioButton crossLineI = findViewById(crossLine.getCheckedRadioButtonId());
TextView autoPowerPortBottom = findViewById(R.id.autoPowerPortBottomDisplay);
TextView autoPowerPortOuter = findViewById(R.id.autoPowerPortOuterDisplay);
TextView autoDroppedPowerCells = findViewById(R.id.autoDroppedPowerCellsDisplay);
RadioGroup autoConflict = findViewById(R.id.autoConflict);
RadioButton autoConflictS = findViewById(autoConflict.getCheckedRadioButtonId());
TextView powerPortBottom = findViewById(R.id.powerPortBottomDisplay);
TextView powerPortOuter = findViewById(R.id.powerPortOuterDisplay);
TextView droppedPowerCells = findViewById(R.id.droppedPowerCellsDisplay);
RadioGroup rotationControl = findViewById(R.id.rotationControlGroup);
RadioButton rotationControlI = findViewById(rotationControl.getCheckedRadioButtonId());
RadioGroup stageReached = findViewById(R.id.stageReachedGroup);
RadioButton stageReachedI = findViewById(stageReached.getCheckedRadioButtonId());
CheckBox boxTrench = findViewById(R.id.boxTrench);
CheckBox boxCenter = findViewById(R.id.boxCenter);
CheckBox boxOnTheirSide = findViewById(R.id.boxOnTheirSide);
RadioGroup climb = findViewById(R.id.climb);
RadioButton climbS = findViewById(climb.getCheckedRadioButtonId());
RadioGroup multiClimb = findViewById(R.id.multiClimb);
RadioButton multiClimbS = findViewById(multiClimb.getCheckedRadioButtonId());
RadioGroup driverSkill = findViewById(R.id.driverSkill);
RadioButton driverSkillI = findViewById(driverSkill.getCheckedRadioButtonId());
RadioGroup focusOnDefense = findViewById(R.id.groupFocusOnDefense);
RadioButton focusOnDefenseI = findViewById(focusOnDefense.getCheckedRadioButtonId());
TextView beached = findViewById(R.id.beachedDisplay);
TextView disabled = findViewById(R.id.disabledDisplay);
TextView died = findViewById(R.id.diedDisplay);
TextView fouls = findViewById(R.id.foulDisplay);
TextView techFouls = findViewById(R.id.techFoulDisplay);
EditText notes = findViewById(R.id.editNotes);
String[] outputString = new String[]{
scoutName.getText().toString(),
teamNumber.getText().toString(),
matchNumber.getText().toString(),
allianceI.getText().toString(),
driverStationI.getText().toString(),
crossLineI.getText().toString(),
autoPowerPortBottom.getText().toString(),
autoPowerPortOuter.getText().toString(),
autoDroppedPowerCells.getText().toString(),
autoConflictS.getText().toString(),
powerPortBottom.getText().toString(),
powerPortOuter.getText().toString(),
droppedPowerCells.getText().toString(),
rotationControlI.getText().toString(),
stageReachedI.getText().toString(),
boxTrench.getText().toString(),
boxCenter.getText().toString(),
boxOnTheirSide.getText().toString(),
climbS.getText().toString(),
multiClimbS.getText().toString(),
driverSkillI.getText().toString(),
focusOnDefenseI.getText().toString(),
beached.getText().toString(),
disabled.getText().toString(),
died.getText().toString(),
fouls.getText().toString(),
techFouls.getText().toString(),
notes.getText().toString()
};
for (int i = 0; i < outputString.length; i++) {
stringBuilder.append(outputString[i]);
stringBuilder.append(", ");
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.minusButtonAutoPowerPortBottom:
subInt(R.id.autoPowerPortBottomDisplay);
break;
case R.id.plusButtonAutoPowerPortBottom:
addInt(R.id.autoPowerPortBottomDisplay);
break;
case R.id.minusButtonAutoPowerPortOuter:
subInt(R.id.autoPowerPortOuterDisplay);
break;
case R.id.plusButtonAutoPowerPortOuter:
addInt(R.id.autoPowerPortOuterDisplay);
break;
case R.id.minusAutoDroppedPowerCells:
subInt(R.id.autoDroppedPowerCellsDisplay);
break;
case R.id.plusAutoDroppedPowerCells:
addInt(R.id.autoDroppedPowerCellsDisplay);
break;
case R.id.minusPowerPortBottom:
subInt(R.id.powerPortBottomDisplay);
break;
case R.id.plusPowerPortBottom:
addInt(R.id.powerPortBottomDisplay);
break;
case R.id.minusPowerPortOuter:
subInt(R.id.powerPortOuterDisplay);
break;
case R.id.plusPowerPortOuter:
addInt(R.id.powerPortOuterDisplay);
break;
case R.id.minusDroppedPowerCells:
subInt(R.id.droppedPowerCellsDisplay);
break;
case R.id.plusDroppedPowerCells:
addInt(R.id.droppedPowerCellsDisplay);
break;
case R.id.minusBeached:
subInt(R.id.beachedDisplay);
break;
case R.id.plusBeached:
addInt(R.id.beachedDisplay);
break;
case R.id.minusDisabled:
subInt(R.id.disabledDisplay);
break;
case R.id.plusDisabled:
addInt(R.id.disabledDisplay);
break;
case R.id.minusDied:
subInt(R.id.minusDied);
break;
case R.id.plusDied:
addInt(R.id.plusDied);
break;
case R.id.minusFouls:
subInt(R.id.foulDisplay);
break;
case R.id.plusFoul:
addInt(R.id.foulDisplay);
break;
case R.id.minusTechFoul:
subInt(R.id.techFoulDisplay);
break;
case R.id.plusTechFoul:
addInt(R.id.techFoulDisplay);
break;
default:
break;
}
}
XML повторяется для каждой отдельной функции (т. Е. BottoPowerPort против количества мертвых объявлений)
<TextView
android:id="@+id/viewAutoPowerPortBottom"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/autoPowerPortBottom"
android:textAppearance="@style/textColor"
android:textSize="24sp"/>
<Button
android:id="@+id/minusButtonAutoPowerPortBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<TextView
android:id="@+id/autoPowerPortBottomDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/zero"
android:textAppearance="@style/textColor"
android:textSize="30sp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/plusButtonAutoPowerPortBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<TextView
android:id="@+id/viewAutoPowerPortOuter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/autoPowerPortOuter"
android:textAppearance="@style/textColor"
android:textSize="24sp"/>
<Button
android:id="@+id/minusButtonAutoPowerPortOuter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<TextView
android:id="@+id/autoPowerPortOuterDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/zero"
android:textAppearance="@style/textColor"
android:textSize="30sp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/plusButtonAutoPowerPortOuter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<TextView
android:id="@+id/viewAutoDroppedPowerCells"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/autoDroppedPowerCells"
android:textAppearance="@style/textColor"
android:textSize="30sp"/>
<Button
android:id="@+id/minusAutoDroppedPowerCells"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<TextView
android:id="@+id/autoDroppedPowerCellsDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/zero"
android:textAppearance="@style/textColor"
android:textSize="30sp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/plusAutoDroppedPowerCells"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>