Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: OnClick Button Doesn't Append/Delete Value to Text View
    text
    copied!<p>I can't make Android <code>onClick</code> events work at all. I've rendered a TextView and some buttons in activity_main.xml. In MainActivity.java I tryied to attach an <code>onClick</code> event to all buttons that will append their value to an input field. However, I'm having trouble finding some tutorials, books, etc on the subject of making dialers work. I have three books and none talk about using the call functions or making dialers. The tutorials I find online are all about people showing users how to root and theme their dialer, not how to program one. So my issues:</p> <p>1) Where am I going wrong with appending a value to my Text view? 2) How will I remove the last value with the del onClick event? 3) How do I stop the Text view from pulling up the keyboard when a user clicks it? I.e. how to make it disabled/readonly (maybe) so only the buttons can update it?</p> <p>Thanks for your help. In the below code I've cut the buttons down to one. All I've done is copy all the buttons and change the IDs, text, etc. So all the methods are the same but I can't get just one button working. Again, please don't laugh at me too hard for the code below.</p> <p>activity_main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/title_two" android:textAppearance="?android:attr/textAppearanceLarge" /&gt; &lt;EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:maxLength="15" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" &gt; &lt;Button android:id="@+id/one" android:layout_width="93dp" android:layout_height="wrap_content" android:text="1" /&gt; &lt;Button android:id="@+id/two" android:layout_width="93dp" android:layout_height="wrap_content" android:text="2" /&gt; &lt;Button android:id="@+id/three" android:layout_width="93dp" android:layout_height="wrap_content" android:text="3" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" &gt; &lt;Button android:id="@+id/four" android:layout_width="93dp" android:layout_height="wrap_content" android:text="4" /&gt; &lt;Button android:id="@+id/five" android:layout_width="93dp" android:layout_height="wrap_content" android:text="5" /&gt; &lt;Button android:id="@+id/six" android:layout_width="93dp" android:layout_height="wrap_content" android:text="6" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" &gt; &lt;Button android:id="@+id/seven" android:layout_width="93dp" android:layout_height="wrap_content" android:text="7" /&gt; &lt;Button android:id="@+id/eight" android:layout_width="93dp" android:layout_height="wrap_content" android:text="8" /&gt; &lt;Button android:id="@+id/nine" android:layout_width="93dp" android:layout_height="wrap_content" android:text="9" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" &gt; &lt;Button android:id="@+id/star" android:layout_width="93dp" android:layout_height="wrap_content" android:text="*" /&gt; &lt;Button android:id="@+id/zero" android:layout_width="93dp" android:layout_height="wrap_content" android:text="0" /&gt; &lt;Button android:id="@+id/pound" android:layout_width="93dp" android:layout_height="wrap_content" android:text="#" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" &gt; &lt;Button android:id="@+id/callButton" android:layout_width="93dp" android:layout_height="wrap_content" android:text="Call" /&gt; &lt;Button android:id="@+id/contacts" android:layout_width="93dp" android:layout_height="wrap_content" android:text="Con" /&gt; &lt;Button android:id="@+id/del" android:layout_width="93dp" android:layout_height="wrap_content" android:text="Del" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>And MainActivity.java:</p> <pre><code>package com.example.dialertest import android.app.Activity; import android.content.Intent; import android.content.ActivityNotFoundException; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { Button oneBtn; Button twoBtn; Button threeBtn; Button fourBtn; Button fiveBtn; Button sixBtn; Button sevenBtn; Button eightBtn; Button nineBtn; Button starBtn; Button zeroBtn; Button poundBtn; Button callBtn; Button conBtn; Button delBtn; EditText numTxt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); oneBtn = (Button) findViewById(R.id.one); twoBtn = (Button) findViewById(R.id.two); threeBtn = (Button) findViewById(R.id.three); fourBtn = (Button) findViewById(R.id.four); fiveBtn = (Button) findViewById(R.id.five); sixBtn = (Button) findViewById(R.id.six); sevenBtn = (Button) findViewById(R.id.seven); eightBtn = (Button) findViewById(R.id.eight); nineBtn = (Button) findViewById(R.id.nine); starBtn = (Button) findViewById(R.id.star); zeroBtn = (Button) findViewById(R.id.zero); poundBtn = (Button) findViewById(R.id.pound); callBtn = (Button) findViewById(R.id.callButton); conBtn = (Button) findViewById(R.id.contacts); delBtn = (Button) findViewById(R.id.del); numTxt = (EditText) findViewById(R.id.editText1); oneBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { oneBtn.setText("1"); numTxt.setText(numTxt.getText() + " " + oneBtn.getText()); //numTxt.append("1"); } }); /**private void performDial(String numberString) { if (!numberString.equals("")) { Uri number = Uri.parse("tel:" + numberString); Intent dial = new Intent(Intent.ACTION_CALL, number); startActivity(dial); } }*/ /*private void call() { try { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel: 8880000000")); startActivity(callIntent); } catch(ActivityNotFoundException e) { Log.e("Dialed Number","Call Failed", e); } } }*/ } } </code></pre>
 

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