【问题标题】:Android Studio - Automatically create java class variables from inflated viewsAndroid Studio - 从膨胀的视图自动创建 java 类变量
【发布时间】:2018-06-03 13:35:27
【问题描述】:

我有很多关于我的活动的视图,我希望在视图膨胀后既能从中获取值,也能将值加载到其中。必须为每个字段创建特定于类的变量、特定于类的 getter 和特定于类的 setter,这很烦人。拥有一个热键或自动创建一个以您正在寻找的视图为目标的变量并为该特定视图类型生成 getter 和 setter 函数的东西似乎应该是显而易见的。

显然,有些视图比其他视图更难为其创建 getter 和 setter,但文本框、复选框等基本字段应该非常容易。

话虽如此,有没有人有什么建议可以让我不要花一整天的时间做体力劳动?

谢谢

【问题讨论】:

  • 跳过 getter 和 setter。
  • 没错,这是一个选择,也许我应该这样做,因为我很生气。创建 getter 和 setter 会稍微干净一些,并且有时会节省重复的代码,但你是对的,它们可能是不必要的。但它仍然没有改变这样一个事实,即我必须为我想在 XML 布局中编辑的每个视图提供一个 dam 视图 ID,然后继续,然后在 java 类中再次为这些完全相同的视图编写多行,一次声明它,然后在布局膨胀后为它提供到实际字段的链接。它只是变得乏味。
  • 感谢大家迄今为止的回复。我已经阅读了它们,它们正是我想要的。

标签: java android variables view


【解决方案1】:

【讨论】:

    【解决方案2】:

    尝试使用 Android 注释。

    举例

    EditText editText;
    TextView textView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        .....
        myEditText = (EditText) findViewById(R.id.passwordEditText);
        textView = (TextView) findViewById(R.id.passwordText);
    }
    

    上面的行可以替换为

    @ViewById(R.id.passwordEditText)
    EditText myEditText;
    
    @ViewById(R.id.passwordText)
    TextView textView;
    

    【讨论】:

      【解决方案3】:

      This 会自动给你 getter setter constructor blah blah 对于视图this 将有助于消除 findviewbyid

      【讨论】:

        【解决方案4】:

        我已经编写了 PHP 代码,只需将您的 xml 文件粘贴到 test.txt(创建后)文件并运行 PHP 代码。您将需要 xamp 在笔记本电脑上运行 php 代码。我很确定可以编写更小更好的代码。

        <?php
        
        $keyword = 'android:id=';
        
        $file = 'test.txt';
        $contents = htmlspecialchars(file_get_contents($file));
        $array = explode("\n", $contents);
        $arrayToSearch = array_map('trim', $array);
        
        
        customSearchIvVariable($keyword, $arrayToSearch);
        echo "</Br></Br>";
        customSearchIvBinding($keyword, $arrayToSearch);
        echo "</Br></Br>";
        customSearchTvVariable($keyword, $arrayToSearch);
        echo "</Br></Br>";
        customSearchTvBinding($keyword, $arrayToSearch);
        
        function customSearchIvVariable($keyword, $arrayToSearch) {
            foreach ($arrayToSearch as $key => $arrayItem) {
        
                if (stristr($arrayItem, $keyword)) {
        
                    $arrayItem = html_entity_decode($arrayItem, ENT_QUOTES, "UTF-8");
                    $arrayItem = str_replace('android:id="@+id/l_people_you_may_like_', '', $arrayItem);
                    $arrayItem = str_replace('"', '', $arrayItem);
                    $arrayItem = str_replace('_', ' ', $arrayItem);
                    $arrayItem = ucwords($arrayItem);
                    $arrayItem = str_replace(' ', '', $arrayItem);
                    $arrayItem = lcfirst($arrayItem);
        
                    if (strpos($arrayItem, 'iv') !== false) {
        
                        echo $arrayItem . ", ";
                    }
                }
            }
        }
        
        function customSearchIvBinding($keyword, $arrayToSearch) {
            foreach ($arrayToSearch as $key => $arrayItem) {
        
                if (stristr($arrayItem, $keyword)) {
        
                    $arrayItem = html_entity_decode($arrayItem, ENT_QUOTES, "UTF-8");
                    $arrayItem = str_replace('android:id="@+id/l_people_you_may_like_', '', $arrayItem);
                    $arrayItem = str_replace('"', '', $arrayItem);
        
                    $arrayBinding = $arrayItem;
        
                    $arrayItem = str_replace('_', ' ', $arrayItem);
                    $arrayItem = ucwords($arrayItem);
                    $arrayItem = str_replace(' ', '', $arrayItem);
                    $arrayItem = lcfirst($arrayItem);
        
                    if (strpos($arrayItem, 'iv') !== false) {
        
                        echo $arrayItem . " ";
                    }
        
                    if (strpos($arrayBinding, 'iv') !== false) {
        
                        echo "= itemView.findViewById(R.id.l_people_you_may_like_" . $arrayBinding . ");" . "</br>";
                    }
                }
            }
        }
        
        function customSearchTvVariable($keyword, $arrayToSearch) {
            foreach ($arrayToSearch as $key => $arrayItem) {
        
                if (stristr($arrayItem, $keyword)) {
        
                    $arrayItem = html_entity_decode($arrayItem, ENT_QUOTES, "UTF-8");
                    $arrayItem = str_replace('android:id="@+id/l_people_you_may_like_', '', $arrayItem);
                    $arrayItem = str_replace('"', '', $arrayItem);
                    $arrayItem = str_replace('_', ' ', $arrayItem);
                    $arrayItem = ucwords($arrayItem);
                    $arrayItem = str_replace(' ', '', $arrayItem);
                    $arrayItem = lcfirst($arrayItem);
        
                    if (strpos($arrayItem, 'tv') !== false) {
        
                        echo $arrayItem . ", ";
                    }
                }
            }
        }
        
        function customSearchTvBinding($keyword, $arrayToSearch) {
            foreach ($arrayToSearch as $key => $arrayItem) {
        
                if (stristr($arrayItem, $keyword)) {
        
                    $arrayItem = html_entity_decode($arrayItem, ENT_QUOTES, "UTF-8");
                    $arrayItem = str_replace('android:id="@+id/l_people_you_may_like_', '', $arrayItem);
                    $arrayItem = str_replace('"', '', $arrayItem);
        
                    $arrayBinding = $arrayItem;
        
                    $arrayItem = str_replace('_', ' ', $arrayItem);
                    $arrayItem = ucwords($arrayItem);
                    $arrayItem = str_replace(' ', '', $arrayItem);
                    $arrayItem = lcfirst($arrayItem);
        
                    if (strpos($arrayItem, 'tv') !== false) {
        
                        echo $arrayItem . " ";
                    }
        
                    if (strpos($arrayBinding, 'tv') !== false) {
        
                        echo "= itemView.findViewById(R.id.l_people_you_may_like_" . $arrayBinding . ");" . "</br>";
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-03
          • 1970-01-01
          • 1970-01-01
          • 2017-07-12
          • 2011-05-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多