在活动开始时循环分配按钮的快速方法是什么?

我有一段代码在我的主活动的开始处onCreate()(之后的第 5 行super.onCreate)被调用,我怀疑它会减慢启动(和调试)。


这是一个循环,用于分配 10 个按钮以及每个按钮的 Click 侦听器。这是片段-


//expostate is a boolean and is always false at start of activity

//checkLength(Screen) will always return true at start of activity

//expression is of String data type

//Screen is a TextView


for(int i=0; i<10; i++){

            String btnid = "btn" + i;

            int resourceid = getResources().getIdentifier(btnid, "id", getPackageName());

            numbuttons[i] = findViewById(resourceid);

            final String value =  String.valueOf(i);

            numbuttons[i].setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {

                    if (checkLength(Screen) && !expostate) {

                        if (expression.equals("0")) {

                            expression = "";

                            Screen.setText("");

                        }

                        Screen.append(value);

                        expression += value;

                        presentop = false;

                    }

                    else if (checkLength(Screen) && expostate) {

                        if (expression.equals("0")) { 

                            expression = "";

                            Screen.setText("");

                        }

                        Screen.append(getSuperscript(value));

                        expression += value;

                        presentop = false;

                    }

                }

            });

        }

有没有更好/更快的方法来完成同样的任务?


POPMUISE
浏览 72回答 3
3回答

弑天下

我建议的改进是方法onClick()。你的逻辑可以这样写:public void onClick(View view) {&nbsp; &nbsp; if (expression.equals("0")) {&nbsp; &nbsp; &nbsp; &nbsp; expression = "";&nbsp; &nbsp; &nbsp; &nbsp; Screen.setText("");&nbsp; &nbsp; }&nbsp; &nbsp; if (checkLength(Screen)) Screen.append(expostate ? getSuperscript(value) : value);&nbsp; &nbsp; expression += value;&nbsp; &nbsp; presentop = false;}

智慧大石

因此,我发现一种不太笨拙的方法是android:onClick为 10 个按钮中的每一个按钮分配一个android:tag提取值。代码:-public void onNumpress(View v){&nbsp; &nbsp; &nbsp; &nbsp; buttonFlash((Button) v);&nbsp; &nbsp; &nbsp; &nbsp; String value = getResources().getResourceEntryName(v.getId()).replace("btn", "");&nbsp; &nbsp; &nbsp; &nbsp; if (expression.equals("0")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expression = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen.setText("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (checkLength(Screen)) Screen.append(expostate ? getSuperscript(value) : value);&nbsp; &nbsp; &nbsp; &nbsp; expression += value;&nbsp; &nbsp; &nbsp; &nbsp; presentop = false;&nbsp; &nbsp; }其中一个按钮的 xml 布局:-&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/btn0"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="78dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="85dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@color/colorPrimary"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/btn0"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/colorSecondary"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="24sp"&nbsp; &nbsp; &nbsp; &nbsp; android:onClick="onNumpress"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toEndOf="@+id/btnans" />

繁花不似锦

如果按钮的 ID 在运行时已知,那么最好只创建一个包含 ID 的数组,而不是执行 getResources() 调用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java