作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个测验应用程序。用户必须完成显示屏上显示的短语并在编辑文本中写下汽车的名称,按下按钮后,如果答案正确,编辑文本变为绿色,否则变为红色。如果所有答案都正确(绿色),则 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/
我正在制作一个测验应用程序。用户必须完成显示屏上显示的短语并在编辑文本中写下汽车的名称,按下按钮后,如果答案正确,编辑文本变为绿色,否则变为红色。如果所有答案都正确(绿色),则 Intent 继续下一
我不知道这是不是该问的地方,但它是编程(在某种程度上)。 Google 标签管理器是如何工作的? 我的看法是: 页面加载 GTM 代码加载并执行此操作 _进行检查 _创建数据层对象 _invokes
我是一名优秀的程序员,十分优秀!