1  /*
2   * Copyright (C) 2007 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.skeletonapp;
18  
19  import android.app.Activity;
20  import android.os.Bundle;
21  import android.view.KeyEvent;
22  import android.view.Menu;
23  import android.view.MenuItem;
24  import android.view.View;
25  import android.view.View.OnClickListener;
26  import android.widget.Button;
27  import android.widget.EditText;
28  
29  /**
30   * This class provides a basic demonstration of how to write an Android
31   * activity. Inside of its window, it places a single view: an EditText that
32   * displays and edits some internal text.
33   */
34  public class SkeletonActivity extends Activity {
35  
36      static final private int BACK_ID = Menu.FIRST;
37      static final private int CLEAR_ID = Menu.FIRST + 1;
38  
39      private EditText mEditor;
40  
SkeletonActivity()41      public SkeletonActivity() {
42      }
43  
44      /** Called with the activity is first created. */
45      @Override
onCreate(Bundle savedInstanceState)46      public void onCreate(Bundle savedInstanceState) {
47          super.onCreate(savedInstanceState);
48  
49          // Inflate our UI from its XML layout description.
50          setContentView(R.layout.skeleton_activity);
51  
52          // Find the text editor view inside the layout, because we
53          // want to do various programmatic things with it.
54          mEditor = (EditText) findViewById(R.id.editor);
55  
56          // Hook up button presses to the appropriate event handler.
57          ((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
58          ((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);
59  
60          mEditor.setText(getText(R.string.main_label));
61      }
62  
63      /**
64       * Called when the activity is about to start interacting with the user.
65       */
66      @Override
onResume()67      protected void onResume() {
68          super.onResume();
69      }
70  
71      /**
72       * Called when your activity's options menu needs to be created.
73       */
74      @Override
onCreateOptionsMenu(Menu menu)75      public boolean onCreateOptionsMenu(Menu menu) {
76          super.onCreateOptionsMenu(menu);
77  
78          // We are going to create two menus. Note that we assign them
79          // unique integer IDs, labels from our string resources, and
80          // given them shortcuts.
81          menu.add(0, BACK_ID, 0, R.string.back).setShortcut('0', 'b');
82          menu.add(0, CLEAR_ID, 0, R.string.clear).setShortcut('1', 'c');
83  
84          return true;
85      }
86  
87      /**
88       * Called right before your activity's option menu is displayed.
89       */
90      @Override
onPrepareOptionsMenu(Menu menu)91      public boolean onPrepareOptionsMenu(Menu menu) {
92          super.onPrepareOptionsMenu(menu);
93  
94          // Before showing the menu, we need to decide whether the clear
95          // item is enabled depending on whether there is text to clear.
96          menu.findItem(CLEAR_ID).setVisible(mEditor.getText().length() > 0);
97  
98          return true;
99      }
100  
101      /**
102       * Called when a menu item is selected.
103       */
104      @Override
onOptionsItemSelected(MenuItem item)105      public boolean onOptionsItemSelected(MenuItem item) {
106          switch (item.getItemId()) {
107          case BACK_ID:
108              finish();
109              return true;
110          case CLEAR_ID:
111              mEditor.setText("");
112              return true;
113          }
114  
115          return super.onOptionsItemSelected(item);
116      }
117  
118      /**
119       * A call-back for when the user presses the back button.
120       */
121      OnClickListener mBackListener = new OnClickListener() {
122          public void onClick(View v) {
123              finish();
124          }
125      };
126  
127      /**
128       * A call-back for when the user presses the clear button.
129       */
130      OnClickListener mClearListener = new OnClickListener() {
131          public void onClick(View v) {
132              mEditor.setText("");
133          }
134      };
135  }
136