首页 > 解决方案 > 如何减少 if else if 梯形代码长度,它根据 if 条件内的文本显示 textviews

问题描述

我是 Android Development 的新手。我创建了一个应用程序,它将在单击文本视图时显示相应的文本视图。

为此,我给出了一个 if-else-if 梯形条件语句来显示文本视图。由于我有很多文本视图(大约 100 个),在构建项目 android studio 时说代码太长。

如何解决这个问题。是否可以使用 MAP 遍历 textview IDS?

请帮我。

 public void click(View v) {

    switch (v.getId()) {
        case R.id.fam:
            // check if a child is set to a specific String
            String toSearchFor = "badaatext";
            boolean found = false;
            ArrayList<View> allViewsWithinMyTopView = getAllChildren(findViewById(R.id.linear));
            for (View child : allViewsWithinMyTopView) {
                if (child instanceof TextView) {
                    TextView childTextView = (TextView) child;
                    int intName = childTextView.getId();
                    System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intName));
                    if (childTextView.getResources().getResourceEntryName(intName).startsWith("badaatext")) {
                        found = true;
                        System.out.println("The text is foudn inside the layout");
                        System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intName));
                        childTextView.setVisibility(View.GONE);

                        String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
                        System.out.println("The Big TextView is : " + BadaaString);
                        String ChotaString = BadaaString.substring(9, 12);
                        System.out.println("The Small Textview is : " + ChotaString);
                        if (ChotaString.equalsIgnoreCase("rel")) {
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.rel);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("nam")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.nam);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mom")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mom);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("dad")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.dad);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("elb")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.elb);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("els")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.els);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("yos")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.yos);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("sil")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.sil);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("yob")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.yob);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("hus")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.hus);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("wif")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.wif);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("fre")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.fre);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("tea")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.tea);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("ser")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.ser);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mai")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mai);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("wom")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.wom);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("stm")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.stm);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("man")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.man);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("boy")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.boy);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gir")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gir);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gue")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gue);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("cus")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.cus);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("hoi")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.hoi);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gif")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gif);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("son")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.son);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("dau")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.dau);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gpa")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gpa);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gma")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gma);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gdp")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gdp);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gdm")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gdm);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("und")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.und);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("aud")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.aud);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("maa")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.maa);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mam")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mam);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mad")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mad);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("ata")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.ata);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("fla")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.fla);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mla")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mla);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("sod")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.sod);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gru")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gru);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("som")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.som);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("grm")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.grm);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("unp")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.unp);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("aup")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.aup);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else{
                            System.out.println("No Matches found");
                        }

                    }
                }
            }
            if (!found) {
                //fail("Text '" + The text is not available in the layout");
                System.out.println("The TextView is not present inside the layout");
            }


            //Make the Clicked View to be Invisible
            TextView Engfam = (TextView) findViewById(R.id.fam);
            Engfam.setVisibility(View.GONE);

            //Adding the corresponding dynamic textview
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            /* Add textview 1 */
            TextView textViewFamily = new TextView(lessonSix.this);
//            textView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
//                    LinearLayout.LayoutParams.MATCH_PARENT));
            LinearLayout.LayoutParams textviewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewFamily, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);

            textviewLayoutParams.setMargins(0, 9, 0, 0);
//                linearLayout.addView(textView1, 1, textviewLayoutParams);
            textViewFamily.setId(R.id.badaatextFam);
            textViewFamily.setLayoutParams(textviewLayoutParams);
            textViewFamily.setBackgroundResource(R.drawable.shapelineinsideroundedrect
 );
textViewFamily.setText("FAMILY - குடும்பம்");
            textViewFamily.setGravity(Gravity.TOP | Gravity.CENTER);
            textViewFamily.setTextSize(20);
            textViewFamily.setTypeface(null, Typeface.BOLD);
            textViewFamily.setPadding(0, 25, 0, 25);
            textViewFamily.setVisibility(View.VISIBLE);
            linearLayout.addView(textViewFamily, 1);
            break;

 case R.id.gir:

            // check if a child is set to a specific String
            found = false;
            ArrayList<View> allViewsWithinMyTopView21 = getAllChildren(findViewById(R.id.linear));
            for (View child : allViewsWithinMyTopView21) {
                if (child instanceof TextView) {
                    childTextView = (TextView) child;
                    intchildName = childTextView.getId();
                    System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intchildName));
                    if (childTextView.getResources().getResourceEntryName(intchildName).startsWith("badaatext")) {
                        found = true;
                        System.out.println("The text is foudn inside the layout");
                        System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intchildName));
                        childTextView.setVisibility(View.GONE);

                        String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
                        System.out.println("The Big TextView is : " + BadaaString);
                        String ChotaString = BadaaString.substring(9, 12);
                        System.out.println("The Small Textview is : " + ChotaString);
//if (ChotaString.equalsIgnoreCase("fam")) {
// //Display the Small TextView:
// TextView Engtxt1 = (TextView) findViewById(R.id.fam);
//Engtxt1.setVisibility(View.VISIBLE);
//}
       int textViewId = context.getResources().getIdentifier(ChotaString, "id", context.getPackageName());
                        TextView Engtxt1 = (TextView) findViewById(textViewId);
                        Engtxt1.setVisibility(View.VISIBLE);

                    }

                }
            }
            if (!found) {
                //fail("Text '" + The text is not available in the layout");
                System.out.println("The TextView is not present inside the layout");
            }
            //Make the Clicked View to be Invisible
            TextView Enggir = (TextView) findViewById(R.id.gir);
            Enggir.setVisibility(View.GONE);


            //Adding the corresponding dynamic textview
            LinearLayout linearLayout21 = (LinearLayout) findViewById(R.id.linear);
            linearLayout21.setOrientation(LinearLayout.VERTICAL);
            /* Add textview 1 */
            TextView textViewGirl = new TextView(lessonSix.this);
//textView1.setLayoutParams(new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
//LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams textviewLayoutParams21 = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewGirl, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);
            textviewLayoutParams21.setMargins(0, 9, 0, 0);
//linearLayout.addView(textView1, 1, textviewLayoutParams);s
            textViewGirl.setId(R.id.badaatextGir);
            textViewGirl.setLayoutParams(textviewLayoutParams21);
            textViewGirl.setBackgroundResource(R.drawable.shapelineinsideroundedrect);
            textViewGirl.setText("GIRL - சிறுமி");
            textViewGirl.setGravity(Gravity.TOP | Gravity.CENTER);
            textViewGirl.setTextSize(20);
            textViewGirl.setTypeface(null, Typeface.BOLD);
            textViewGirl.setPadding(0, 25, 0, 25);
            textViewGirl.setVisibility(View.VISIBLE);
            linearLayout21.addView(textViewGirl, 40);
            break;

相关的 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context=".lessonSix">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--<TextView-->
        <!--android:id="@+id/badaatextOne"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="130dp"-->
        <!--android:layout_marginTop="5dp"-->
        <!--android:paddingTop="25dp"-->
        <!--android:background="@drawable/shapelineinsideroundedrect"-->
        <TextView
            android:id="@+id/fam"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shaperoundedcornerrect"
            android:text="FAMILY"
            android:onClick="click"
            android:clickable="true"
            android:gravity="center"
            android:textSize="20dip"
            android:textStyle="bold"
            tools:ignore="OnClick" />

        <TextView
            android:id="@+id/rel"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shaperoundedcornerrect"
            android:text="RELATION"
            android:clickable="true"
            android:onClick="click"
            android:gravity="center"
            android:textSize="20dip"
            android:textStyle="bold" />
 <TextView
            android:id="@+id/gir"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shaperoundedcornerrect"
            android:text="GIRL"
            android:clickable="true"
            android:onClick="click"
            android:gravity="center"
            android:textSize="20dip"
            android:textStyle="bold"/>

</LinearLayout>
</ScrollView>

</RelativeLayout>

添加异常:致命异常:主进程:com.seekho.noushad.dictionary,PID:30013 java.lang.IllegalStateException:无法在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick( AppCompatViewInflater.java:293) 在 android.view.View.performClick(View.java:4788) 在 android.view.View$PerformClick.run(View.java:19923) 在 android.os.Handler.handleCallback(Handler.java :739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5401) 在 java .lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919)在 com.android.internal.os。ZygoteInit.main(ZygoteInit.java 引起:java.lang.reflect.InvocationTargetException 应用程序终止。

New Log(Exception): 

在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) 在 com.android.internal 的 java.lang.reflect.Method.invoke(Method.java:372) 调用(本机方法)。 os.ZygoteInit.main(ZygoteInit.java:714) I/Process:发送信号。PID:24918 SIG:9 应用程序终止。

标签: androidandroid-studiotextview

解决方案


您可以TextView通过其名称获取 Id。由于您的if()条件字符串等于的名称TextView,您可以使用:

int textViewId = context.getResources().getIdentifier(ChotaString, "id", context.getPackageName());
TextView Engtxt1 = (TextView) findViewById(textViewId);
Engtxt1.setVisibility(View.VISIBLE);

而不是整个你的if(){}else{}


推荐阅读