Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. /*
  2. * Copyright 2015, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package com.example.android.testing.espresso.CustomMatcherSample;
  18.  
  19. import android.app.Activity;
  20. import android.os.Bundle;
  21. import android.support.annotation.VisibleForTesting;
  22. import android.view.View;
  23. import android.widget.EditText;
  24.  
  25. import java.util.Arrays;
  26. import java.util.List;
  27.  
  28. /**
  29. * Asks the user for a coffee preparation and shows if it is valid.
  30. * <p>Valid inputs are anything ending in "coffee" or strings listed in
  31. * {@link MainActivity#COFFEE_PREPARATIONS} The check is always case-insensitive.
  32. * </p>
  33. */
  34. public class MainActivity extends Activity implements View.OnClickListener {
  35.  
  36. @VisibleForTesting
  37. public static final List<String> COFFEE_PREPARATIONS =
  38. Arrays.asList("Schokolade", "Erdbeere", "Banane", "Kaugummi");
  39.  
  40. @VisibleForTesting
  41. public static final String VALID_ENDING = "eis";
  42.  
  43. private EditText mInputText;
  44. private View mSuccessView;
  45. private View mErrorView;
  46.  
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_main);
  51.  
  52. // Sets the listener for the button.
  53. findViewById(R.id.button).setOnClickListener(this);
  54.  
  55. // Get references to the EditText and views showing the result.
  56. mInputText = (EditText) findViewById(R.id.editText);
  57. mSuccessView = findViewById(R.id.inputValidationSuccess);
  58. mErrorView = findViewById(R.id.inputValidationError);
  59. }
  60.  
  61. @Override
  62. public void onClick(View view) {
  63. if (view.getId() == R.id.button) {
  64. // The View to display depends on whether the input is valid or not.
  65. final String inputText = mInputText.getText().toString();
  66.  
  67. // Validate the input and show the result.
  68. showResult(validateText(inputText));
  69. }
  70. }
  71.  
  72. private void showResult(boolean isValidResult) {
  73. mSuccessView.setVisibility(isValidResult ?View.VISIBLE : View.GONE);
  74. mErrorView.setVisibility(isValidResult ? View.GONE : View.VISIBLE);
  75. }
  76.  
  77. private static boolean validateText(String inputText) {
  78. // Every input ending in VALID_ENDING will return true.
  79. if (inputText.toLowerCase().endsWith(VALID_ENDING)) {
  80. return true;
  81. }
  82.  
  83. // Check if the string is in the list.
  84. for (String preparation : COFFEE_PREPARATIONS) {
  85. if (preparation.equalsIgnoreCase(inputText)) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91.  
  92.  
  93. }
  94.  
  95. <?xml version="1.0" encoding="UTF-8"?>
  96. <!--
  97. Copyright 2015, The Android Open Source Project
  98.  
  99. Licensed under the Apache License, Version 2.0 (the "License");
  100. you may not use this file except in compliance with the License.
  101. You may obtain a copy of the License at
  102.  
  103. http://www.apache.org/licenses/LICENSE-2.0
  104.  
  105. Unless required by applicable law or agreed to in writing, software
  106. distributed under the License is distributed on an "AS IS" BASIS,
  107. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  108. See the License for the specific language governing permissions and
  109. limitations under the License.
  110. -->
  111. <resources>
  112.  
  113. <string name="app_name">Laboraufgabe App Testing</string>
  114. <string name="instructions">Bla</string>
  115. <string name="hint"></string>
  116. <string name="validate">validate</string>
  117. <string name="good_choice">Super!</string>
  118. <string name="bad_choice">Ups! Da ist etwas schief gelaufen, versuche es mal mit Schokolade oder Banane</string>
  119.  
  120. </resources>
  121.  
  122. <!--
  123. Copyright 2015, The Android Open Source Project
  124.  
  125. Licensed under the Apache License, Version 2.0 (the "License");
  126. you may not use this file except in compliance with the License.
  127. You may obtain a copy of the License at
  128.  
  129. http://www.apache.org/licenses/LICENSE-2.0
  130.  
  131. Unless required by applicable law or agreed to in writing, software
  132. distributed under the License is distributed on an "AS IS" BASIS,
  133. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  134. See the License for the specific language governing permissions and
  135. limitations under the License.
  136. -->
  137. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  138. xmlns:tools="http://schemas.android.com/tools"
  139. android:layout_width="match_parent"
  140. android:layout_height="match_parent"
  141. android:paddingBottom="@dimen/activity_vertical_margin"
  142. android:paddingLeft="@dimen/activity_horizontal_margin"
  143. android:paddingRight="@dimen/activity_horizontal_margin"
  144. android:paddingTop="@dimen/activity_vertical_margin"
  145. android:orientation="vertical"
  146. tools:context=".MainActivity">
  147.  
  148. <EditText
  149. android:id="@+id/editText"
  150. android:layout_width="match_parent"
  151. android:layout_height="wrap_content"
  152. android:layout_marginTop="48dp"
  153. android:ems="10"
  154. android:hint="@string/hint"/>
  155.  
  156. <Button
  157. android:id="@+id/button"
  158. android:layout_width="wrap_content"
  159. android:layout_height="wrap_content"
  160. android:text="@string/validate"
  161. android:layout_gravity="end"/>
  162.  
  163. <TextView
  164. android:id="@+id/inputValidationSuccess"
  165. android:layout_width="wrap_content"
  166. android:layout_height="wrap_content"
  167. android:layout_marginTop="48dp"
  168. android:background="@drawable/correct"
  169. android:text="@string/good_choice"
  170. android:textAppearance="?android:attr/textAppearanceLarge"
  171. android:visibility="gone"
  172. android:layout_gravity="center_horizontal"/>
  173.  
  174. <TextView
  175. android:id="@+id/inputValidationError"
  176. android:layout_width="wrap_content"
  177. android:layout_height="wrap_content"
  178. android:layout_marginTop="48dp"
  179. android:background="@drawable/incorrect"
  180. android:text="@string/bad_choice"
  181. android:textAppearance="?android:attr/textAppearanceLarge"
  182. android:visibility="gone"
  183. android:layout_gravity="center_horizontal"/>
  184.  
  185.  
  186. <RadioGroup
  187. android:layout_width="match_parent"
  188. android:layout_height="match_parent"
  189. android:orientation="horizontal">
  190.  
  191. <RadioButton
  192. android:id="@+id/radioBecher"
  193. android:layout_height="wrap_content"
  194. android:layout_weight="1"
  195. android:checked="false"
  196. android:text="Im Becher" />
  197.  
  198. <RadioButton
  199. android:id="@+id/radioWaffel"
  200. android:layout_height="wrap_content"
  201. android:layout_weight="1"
  202. android:text="In der Waffel" />
  203. </RadioGroup>
  204.  
  205.  
  206. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement