gpt4 book ai didi

android - 代码优化。 (建筑学)

转载 作者:行者123 更新时间:2023-11-29 00:08:30 26 4
gpt4 key购买 nike

我正在制作一个测验应用程序。用户必须完成显示屏上显示的短语并在编辑文本中写下汽车的名称,按下按钮后,如果答案正确,编辑文本变为绿色,否则变为红色。如果所有答案都正确(绿色),则 Intent 继续下一个 Activity 。

问题是:如何优化我的代码,我不喜欢它的样子?如果我决定添加更多选项,它将无法阅读。

public class MainActivity extends AppCompatActivity {

EditText et_one_one, et_one_two, et_one_three;
Button buttonCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_one_one = (EditText) findViewById(R.id.et_one_one);
et_one_two = (EditText) findViewById(R.id.et_one_two);
et_one_three = (EditText) findViewById(R.id.et_one_three);

buttonCheck = (Button) findViewById(R.id.buttonCheck);

buttonCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean allAnswersCorrect = true;
String t1 = et_one_one.getText().toString().toLowerCase();
String t2 = et_one_two.getText().toString().toLowerCase();
String t3 = et_one_three.getText().toString().toLowerCase();
if (t1.equals("maserati")){
et_one_one.setBackgroundColor(Color.GREEN);
}
else {
allAnswersCorrect = false;
et_one_one.setBackgroundColor(Color.RED);
}
if (t2.equals("mercedes")){
et_one_two.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_two.setBackgroundColor(Color.RED);
}
if (t3.equals("bmw")){
et_one_three.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_three.setBackgroundColor(Color.RED);
}
if(allAnswersCorrect) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
});
}

在我的布局中,我使用 ScrollView:

<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" tools:context=".MainActivity"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView2" >

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/task1"
android:id="@+id/textView1"
android:textSize="20sp"
android:textColor="#010101" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="@string/one_one"
android:id="@+id/textView2" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_one_one"
android:inputType="textCapSentences"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="@string/one_two"
android:id="@+id/textView3" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_one_two"
android:inputType="textCapSentences"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="@string/one_three"
android:id="@+id/textView4" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_one_three"
android:inputType="textCapSentences"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="@string/one_four"
android:id="@+id/textView5" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_one_four"
android:inputType="textCapSentences"/>

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="@string/one_five"
android:id="@+id/textView6" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_one_five"
android:inputType="textCapSentences"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="@+id/buttonCheck" />
</LinearLayout>
</LinearLayout>
</ScrollView>

</LinearLayout>

最佳答案

我强烈推荐 JakeWharton 的 Butterknife 库:

http://jakewharton.github.io/butterknife/

在您的情况下,您的代码将如下所示:

public class MainActivity extends AppCompatActivity {

@Bind(R.id.et_one_one) EditText et_one_one;
@Bind(R.id.et_one_two) EditText et_one_two;
@Bind(R.id.et_one_three) EditText et_one_three;
@Bind(R.id.buttonCheck) Button buttonCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
...
}

@OnClick(R.id.submit)
public void submit(View view) {
// check code here
}

您还可以将所有编辑文本分组到一个组中:

@Bind({ R.id.et_one_one, R.id.et_one_two, R.id.et_one_three })
List<EditText> nameViews;

并对它们应用一些 setter 或操作:

...
ButterKnife.apply(nameViews, LOWERCASE);
...

static final ButterKnife.Action<View> LOWERCASE= new ButterKnife.Action<View>() {
@Override public void apply(View view, int index) {
// TODO set the text of the view to lowercase and disable textview
}
};

关于android - 代码优化。 (建筑学),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32035998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com