【问题标题】:Code optimisation. (Architecture)代码优化。 (建筑学)
【发布时间】:2015-08-16 23:10:36
【问题描述】:

我正在制作一个测验应用程序。用户必须完成显示屏上显示的短语并在edittext中写入汽车名称,按下按钮后,如果回答正确,edittext变为绿色,如果不正确,变为红色。如果所有答案都正确(绿色),则意图继续下一个活动。

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

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>

【问题讨论】:

标签: android optimization


【解决方案1】:

我强烈建议 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;

并对它们应用一些设置器或操作:

...
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
  }
};

【讨论】:

    【解决方案2】:

    您可以使用RecyclerView 来实现这一点。有了它,您可以根据需要为汽车动态添加尽可能多的 EditText。示例如下:

    在您的活动中:

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private String[] answerArray;
    
    Button buttonCheck;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
    
        answerArray = new String[]{
    
            "maserati",
            "mercedes",
            "bmw"
            ... (you can add as much as you want)
        }
    
        buttonCheck = (Button) findViewById(R.id.buttonCheck);
    
    
    
    }
    

    对于 RecyclerView.Adapter

    public class MyRecyclerAdapter extends   RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
    
    String[] mAnswerArray;
    
    
    public static class ViewHolder extends RecyclerView.ViewHolder {
    
        public EditText editText;
    
        public ViewHolder(View v) {
            super(v);
            editText = (EditText) v.findViewById(R.id.editText);
    
        }
    }
    
    public MyRecyclerAdapter(String[] answerArray) {
    
        this.mAnswerArray = answerArray;
    }
    
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position){
    
        super.onBindViewHolder(holder, position);
    
        final String answer = mAnswerArray.get(position);
    
        if ( holder.editText.getText().toString().toLowerCase().equals(answer) ) {
    
            holder.editText.setBackgroundColor(Color.GREEN);
    
    
        } else {
    
            holder.editText.setBackgroundColor(Color.RED);
    
        }
    
    }
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_list_item, parent, false);
    
        ViewHolder vh = new ViewHolder(v);
    
        return vh;
    }
    
    @Override
    public int getItemCount() {
        return mAnswerArray.size();
    }
    
    
    }
    

    对于“my_list_item.xml”,您只需要放入其中,对于ButtonCheck,您也可以将数组或标志传递给适配器以记录每个答案的状态(正确或错误)以决定是否去SecondActivity。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多