代码之家  ›  专栏  ›  技术社区  ›  northerner

如何使所有编辑文本不可编辑(反之亦然)?

  •  -1
  • northerner  · 技术社区  · 8 年前

    EditText ?

    例如,我想要一个替代方案

    EditText Et = findViewById((R.id.one));
    Et.setFocusable(false);
    Et.setClickable(false);
    Et = findViewById((R.id.two));
    Et.setFocusable(false);
    Et.setClickable(false);
    //...continue the pattern
    Et = findViewById((R.id.ten));
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   user8959091 user8959091    8 年前

    把它们排成一列:

    EditText[] etArray = { findViewById(R.id.one), findViewById(R.id.two), findViewById(R.id.three) };
    
    for (EditText et : etArray) {
        et.setFocusable(false);
        et.setClickable(false);
    }
    

    或者如果名字像EditText1,EditText2,。。对于10个视图:

        for (int i = 1; i <= 10; i++) {
            int id = getResources().getIdentifier("EditText" + i, "id", getPackageName());
            EditText et = findViewById(id);
            et.setFocusable(false);
            et.setClickable(false);
        }
    

    onCreate() 并使用列表:

    ArrayList<EditText> etArrayList;
    

    这是给onCreate()的:

    etArrayList = new  ArrayList<EditText>();
    for (int i = 1; i <= 10; i++) {
        int id = getResources().getIdentifier("EditText" + i, "id", getPackageName());
        EditText et = findViewById(id);
        etArrayList.add(et);
    }
    

    在你的活动课上的任何地方使用它:

    for (EditText et : etArrayList) {
        et.setFocusable(false);
        et.setClickable(false);
    }
    
        2
  •  0
  •   Lino    8 年前

    LinearLayout 命名的 main_content ,然后您可以按以下方式循环它们:

    LinearLayout layout = findViewById(R.id.main_content);
    for (int i = 0; i < layout.getChildCount(); i++) {
           View widget = layout.getChildAt(i);
           if (widget instanceof EditText) {
               widget.setFocusable(false);
               widget.setClickable(false);
           }
    }