Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>OnClickListener()</code>. You don't have to specify <code>View</code>. Also, make sure you use a capital 'C' in the overridden <code>onClick()</code> method. Make sure you include <code>import android.view.View.OnClickListener;</code> in your imports section. Eclipse should this for you, but if it's not or you are using Eclipse, add it manually to the top of the class. If you simply do the following, that should work for you:</p> <pre><code> welcome.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int v1 = Integer.parseInt(e.getText().toString()); int v2 = Integer.parseInt(e2.getText().toString()); int v3 = v1 + v2; Toast.makeText(MainActivity.this, "= " + v3, Toast.LENGTH_LONG).show(); return true; } }); </code></pre> <p>--------EDITED FOR FULL EXAMPLE--------</p> <p>In your project, you have your main activity. That activity should use a layout from an XML resource. That XML resource likely looks something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" /&gt; &lt;EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>In your activity class, you have to set that resource as the content view for the activity, using <code>setContentView(resID)</code>. Say your XML file is called helloworld.xml, you would execute <code>setContentView(R.layout.helloworld)</code> in the activity's <code>onCreate(Bundle s)</code> method right after calling <code>super.onCreate(s)</code>.</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.helloworld); } </code></pre> <p>Once you have set the activity's view, you can access that layout's elements (EditTexts, Buttons, etc). To do this, you'll have to create EditText and Button objects (which you were already doing in your posted code, we just have to do them elsewhere instead). Continuing with my example, you could do the following in your <code>onCreate(Bundle s)</code> function:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.helloworld); EditText et1 = (EditText) findViewById(R.id.edittext1); EditText et2 = (EditText) findViewById(R.id.edittext2); Button but = (Button) findViewById(R.id.button1); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) et2.setText(et1.getText().toString()); // this will set the second EditText's text to whatever is in // the first EditText, but you could do anything with the value. } } } </code></pre> <p>If you want to change what the button does based on a menu item selection, you have to override <code>onOptionsItemSelected(MenuItem item)</code> in addition to <code>onCreateOptionsMenu(Menu menu)</code>. <code>onCreateOptionsMenu(Menu menu)</code> simply creates the menu for the menu button to open. <code>onOptionsItemSelected(MenuItem item)</code> actually decides what to do when you select a menu item.</p> <p>See the tutorial at this page for full rundown <a href="http://developer.android.com/guide/topics/ui/menus.html" rel="nofollow">http://developer.android.com/guide/topics/ui/menus.html</a>, but here are their examples with some explanation. These examples are not from my example app above but from the Android Developer API pages. I strongly recommend you go over their tutorial.</p> <p>All you have to do in <code>onCreateOptionsMenu(Menu menu)</code> is tell Android where to get the menu and to inflate that resource. This means there is an XML file called <code>game_menu</code> in the <code>menu</code> folder of the <code>res</code> folder in your project's directory.</p> <pre><code>@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; } </code></pre> <p><code>onOptionsItemSelected(MenuItem item)</code> is where the meat of the menu logic goes, letting you perform different tasks based on which menu item get's selected. In this example, the <code>game_menu</code> XML file mentioned in the above function has menu items called <code>new_game</code> and <code>help</code>.</p> <pre><code>@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload