Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.example.android.testing.espresso.CustomMatcherSample;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.annotation.VisibleForTesting;
- import android.view.View;
- import android.widget.EditText;
- import java.util.Arrays;
- import java.util.List;
- /**
- * Asks the user for a coffee preparation and shows if it is valid.
- * <p>Valid inputs are anything ending in "coffee" or strings listed in
- * {@link MainActivity#COFFEE_PREPARATIONS} The check is always case-insensitive.
- * </p>
- */
- public class MainActivity extends Activity implements View.OnClickListener {
- @VisibleForTesting
- public static final List<String> COFFEE_PREPARATIONS =
- Arrays.asList("Schokolade", "Erdbeere", "Banane", "Kaugummi");
- @VisibleForTesting
- public static final String VALID_ENDING = "eis";
- private EditText mInputText;
- private View mSuccessView;
- private View mErrorView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Sets the listener for the button.
- findViewById(R.id.button).setOnClickListener(this);
- // Get references to the EditText and views showing the result.
- mInputText = (EditText) findViewById(R.id.editText);
- mSuccessView = findViewById(R.id.inputValidationSuccess);
- mErrorView = findViewById(R.id.inputValidationError);
- }
- @Override
- public void onClick(View view) {
- if (view.getId() == R.id.button) {
- // The View to display depends on whether the input is valid or not.
- final String inputText = mInputText.getText().toString();
- // Validate the input and show the result.
- showResult(validateText(inputText));
- }
- }
- private void showResult(boolean isValidResult) {
- mSuccessView.setVisibility(isValidResult ?View.VISIBLE : View.GONE);
- mErrorView.setVisibility(isValidResult ? View.GONE : View.VISIBLE);
- }
- private static boolean validateText(String inputText) {
- // Every input ending in VALID_ENDING will return true.
- if (inputText.toLowerCase().endsWith(VALID_ENDING)) {
- return true;
- }
- // Check if the string is in the list.
- for (String preparation : COFFEE_PREPARATIONS) {
- if (preparation.equalsIgnoreCase(inputText)) {
- return true;
- }
- }
- return false;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Copyright 2015, The Android Open Source Project
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <resources>
- <string name="app_name">Laboraufgabe App Testing</string>
- <string name="instructions">Bla</string>
- <string name="hint"></string>
- <string name="validate">validate</string>
- <string name="good_choice">Super!</string>
- <string name="bad_choice">Ups! Da ist etwas schief gelaufen, versuche es mal mit Schokolade oder Banane</string>
- </resources>
- <!--
- Copyright 2015, The Android Open Source Project
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <EditText
- android:id="@+id/editText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="48dp"
- android:ems="10"
- android:hint="@string/hint"/>
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/validate"
- android:layout_gravity="end"/>
- <TextView
- android:id="@+id/inputValidationSuccess"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="48dp"
- android:background="@drawable/correct"
- android:text="@string/good_choice"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:visibility="gone"
- android:layout_gravity="center_horizontal"/>
- <TextView
- android:id="@+id/inputValidationError"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="48dp"
- android:background="@drawable/incorrect"
- android:text="@string/bad_choice"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:visibility="gone"
- android:layout_gravity="center_horizontal"/>
- <RadioGroup
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <RadioButton
- android:id="@+id/radioBecher"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="false"
- android:text="Im Becher" />
- <RadioButton
- android:id="@+id/radioWaffel"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="In der Waffel" />
- </RadioGroup>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement