我正在学习android,并且在disable
id btnWicketIndia
的值为TextView
的情况下,在wicketsIndiaText
的按钮" \ 10 "
中苦苦挣扎。
还请告诉我我编写代码的方式是正确的还是可以优化的。
package me.jatinsoni.cricketscorecard;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int scoreIndia = 0;
int wicketsIndia = 0;
int scoreSA = 0;
int wicketsSA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// India Buttons
Button btnSixerIndia = findViewById(R.id.sixerIndia);
Button btnFourIndia = findViewById(R.id.fourIndia);
Button btnSingleIndia = findViewById(R.id.singleIndia);
Button btnTwoIndia = findViewById(R.id.twoIndia);
Button btnNoIndia = findViewById(R.id.noIndia);
Button btnWideIndia = findViewById(R.id.wideIndia);
Button btnWicketIndia = findViewById(R.id.wicketIndia);
// South Africa Buttons
Button btnSixerSA = findViewById(R.id.sixerSA);
Button btnFourSA = findViewById(R.id.fourSA);
Button btnSingleSA = findViewById(R.id.singleSA);
Button btnTwoSA = findViewById(R.id.twoSA);
Button btnNoSA = findViewById(R.id.noSA);
Button btnWideSA = findViewById(R.id.wideSA);
Button btnWicketSA = findViewById(R.id.wicketSA);
// Reset button
Button btnResetScore = findViewById(R.id.resetScore);
btnSixerIndia.setOnClickListener(this);
btnFourIndia.setOnClickListener(this);
btnSingleIndia.setOnClickListener(this);
btnTwoIndia.setOnClickListener(this);
btnNoIndia.setOnClickListener(this);
btnWideIndia.setOnClickListener(this);
btnWicketIndia.setOnClickListener(this);
btnSixerSA.setOnClickListener(this);
btnFourSA.setOnClickListener(this);
btnSingleSA.setOnClickListener(this);
btnTwoSA.setOnClickListener(this);
btnNoSA.setOnClickListener(this);
btnWideSA.setOnClickListener(this);
btnWicketSA.setOnClickListener(this);
btnResetScore.setOnClickListener(this);
displayScoreIndia(scoreIndia, wicketsIndia);
displayScoreSA(scoreSA, wicketsSA);
}
@Override
public void onClick(View v) {
// Get Buttons IDs
switch (v.getId()) {
// India Buttons
case R.id.sixerIndia: {
scoreIndia += 6;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
case R.id.fourIndia: {
scoreIndia += 4;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
case R.id.singleIndia: {
scoreIndia += 1;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
case R.id.twoIndia: {
scoreIndia += 2;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
case R.id.noIndia: {
scoreIndia += 1;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
case R.id.wideIndia: {
scoreIndia += 1;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
case R.id.wicketIndia: {
if (String.valueOf(wicketsIndia).equals(" / 10")) {
btnWicketIndia.setEnabled(false);
}
wicketsIndia += 1;
displayScoreIndia(scoreIndia, wicketsIndia);
break;
}
// South Africa Buttons
case R.id.sixerSA: {
scoreSA += 6;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.fourSA: {
scoreSA += 4;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.singleSA: {
scoreSA += 1;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.twoSA: {
scoreSA += 2;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.noSA: {
scoreSA += 1;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.wideSA: {
scoreSA += 1;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.wicketSA: {
wicketsSA += 1;
displayScoreSA(scoreSA, wicketsSA);
break;
}
case R.id.resetScore: {
scoreIndia = 0;
scoreSA = 0;
wicketsIndia = 0;
wicketsSA = 0;
displayScoreIndia(scoreIndia, wicketsIndia);
displayScoreSA(scoreSA, wicketsSA);
break;
}
}
}
@SuppressLint("SetTextI18n")
public void displayScoreIndia(int score, int wickets) {
TextView scoreIndiaText = findViewById(R.id.scoreIndiaText);
TextView wicketsIndiaText = findViewById(R.id.wicketsIndiaText);
scoreIndiaText.setText(String.valueOf(score));
wicketsIndiaText.setText(" / " + String.valueOf(wickets));
}
@SuppressLint("SetTextI18n")
public void displayScoreSA(int score, int wickets) {
TextView scoreSAText = findViewById(R.id.scoreSAText);
TextView wicketsSAText = findViewById(R.id.wicketsSAText);
scoreSAText.setText(String.valueOf(score));
wicketsSAText.setText(" / " + String.valueOf(wickets));
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/cricketground" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<!-- INDIA COLUMN -->
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_marginTop="16dp"
android:src="@drawable/ic_india" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INDIA"
android:textColor="#FFFFFF"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="32dp"
android:gravity="center_horizontal">
<TextView
android:id="@+id/scoreIndiaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="40sp"
android:fontFamily="sans-serif-thin"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/wicketsIndiaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / 0"
android:textSize="20sp"
android:fontFamily="sans-serif-thin"
android:textColor="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/sixerIndia"
style="@style/CircleButton"
android:text="Sixer"/>
<Button
android:id="@+id/fourIndia"
style="@style/CircleButton"
android:text="Four"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/singleIndia"
style="@style/CircleButton"
android:text="Single"/>
<Button
android:id="@+id/twoIndia"
style="@style/CircleButton"
android:text="Two"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/noIndia"
style="@style/CircleButton"
android:text="No"/>
<Button
android:id="@+id/wideIndia"
style="@style/CircleButton"
android:text="Wide"/>
</LinearLayout>
<Button
android:id="@+id/wicketIndia"
style="@style/RoundedButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_gravity="center_horizontal"
android:text="wicket" />
</LinearLayout>
<!-- PARTITION -->
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#33FFFFFF" />
<!--SOUTH AFRICA COLUMN -->
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_marginTop="16dp"
android:src="@drawable/ic_south_africa" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOUTH AFRICA"
android:textColor="#FFFFFF"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="32dp"
android:gravity="center_horizontal">
<TextView
android:id="@+id/scoreSAText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="40sp"
android:fontFamily="sans-serif-thin"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/wicketsSAText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / 0"
android:textSize="20sp"
android:fontFamily="sans-serif-thin"
android:textColor="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/sixerSA"
style="@style/CircleButton"
android:text="Sixer"/>
<Button
android:id="@+id/fourSA"
style="@style/CircleButton"
android:text="Four"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/singleSA"
style="@style/CircleButton"
android:text="Single"/>
<Button
android:id="@+id/twoSA"
style="@style/CircleButton"
android:text="Two"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/noSA"
style="@style/CircleButton"
android:text="No"/>
<Button
android:id="@+id/wideSA"
style="@style/CircleButton"
android:text="Wide"/>
</LinearLayout>
<Button
android:id="@+id/wicketSA"
style="@style/RoundedButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_gravity="center_horizontal"
android:text="wicket" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/resetScore"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset"
android:textColor="#FFFFFF"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
答案 0 :(得分:1)
您正在比较int
。因此,只需使用
if (wicketsIndia == 10) {
btnWicketIndia.setEnabled(false);
}
编辑:
int wicketsSA = 0;
Button btnWicketIndia; // DECLARE IT HERE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
btnWicketIndia = findViewById(R.id.wicketIndia); // ASSIGN HERE
...
}
答案 1 :(得分:1)
我将看看您选择的模式。
使用ViewModel来保存“主队”和“客队”的得分和检票口。
让您的活动/小工具中的数据具有一些观察者,并对更改做出响应。
您需要将Google / Android依赖项添加到您的App Gradle文件:
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
这里是一个示例,它使用类似观察者的模式显示ViewModel和Activity:
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
//Extend FRAGMENT ACTIVITY
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get handle on the ViewModel:
final ViewModelMain viewModelMain = ViewModelProviders.of(this).get(ViewModelMain.class);
//Observe the data in the viewmodel class:
viewModelMain.homeWickets.observe(this, new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
//MAKE CHANGES WHEN THE VALUE FOR THE HOME WICKETS CHANGE LIKE SET ON CLICK METHODS TO NULL?
}
});
viewModelMain.homeScore.observe(this, new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
//MAKE CHANGES WHEN THE VALUE FOR THE HOME SCORE CHANGE LIKE SET ON CLICK METHODS TO NULL?
}
});
//HOW TO SET A VIEW MODEL VALUE LIKE AFTER CLICKING A BUTTON:
View v = new View(getApplicationContext());
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewModelMain.setHomeScore(viewModelMain.getHomeScore().getValue() + 1);
}
});
}
}
import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
public class ViewModelMain extends AndroidViewModel {
public ViewModelMain(@NonNull Application application) {
super(application);
// Set all values to 0 when constructed?
this.awayScore.postValue(0);
this.homeScore.postValue(0);
this.homeWickets.postValue(0);
this.awayWickets.postValue(0);
}
public MutableLiveData<Integer> awayScore = new MutableLiveData<>();
public MutableLiveData<Integer> homeScore = new MutableLiveData<>();
public MutableLiveData<Integer> homeWickets = new MutableLiveData<>();
public MutableLiveData<Integer> awayWickets = new MutableLiveData<>();
//SETTERS -> POST THE DATA TO THE MUTABLE FIELDS
public void setAwayScore(Integer awayScore) {
this.awayScore.postValue(awayScore);
}
public void setAwayWickets(Integer awayWickets) {
this.awayWickets.postValue(awayWickets);
}
public void setHomeScore(Integer homeScore) {
this.homeScore.postValue(homeScore);
}
public void setHomeWickets(Integer homeWickets) {
this.homeWickets.postValue(homeWickets);
}
//STANDARD JAVA GETTERS
public MutableLiveData<Integer> getAwayScore() {
return awayScore;
}
public MutableLiveData<Integer> getAwayWickets() {
return awayWickets;
}
public MutableLiveData<Integer> getHomeScore() {
return homeScore;
}
public MutableLiveData<Integer> getHomeWickets() {
return homeWickets;
}
}
答案 2 :(得分:0)
\
是一个特殊字符。因此,如果您收到包含\
的文本,则必须在前面添加\
。
if (wicketsIndiaText.getText().toString().equals(" \\ 10 ")) {
btnWicketIndia.setEnabled(false);
}