简体   繁体   中英

Android: OnClick Button Doesn't Append/Delete Value to Text View

I can't make Android onClick events work at all. I've rendered a TextView and some buttons in activity_main.xml. In MainActivity.java I tryied to attach an onClick 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:

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? Ie how to make it disabled/readonly (maybe) so only the buttons can update it?

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.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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" >

<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" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:maxLength="15" >
    <requestFocus />
</EditText>

<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >
    <Button
        android:id="@+id/one"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="1" />
    <Button
        android:id="@+id/two"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="2" />
    <Button
        android:id="@+id/three"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="3" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >
    <Button
        android:id="@+id/four"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="4" />
    <Button
        android:id="@+id/five"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="5" />
    <Button
        android:id="@+id/six"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="6" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >
    <Button
        android:id="@+id/seven"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="7" />
    <Button
        android:id="@+id/eight"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="8" />
    <Button
        android:id="@+id/nine"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="9" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >
    <Button
        android:id="@+id/star"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="*" />
    <Button
        android:id="@+id/zero"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="0" />
    <Button
        android:id="@+id/pound"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="#" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >
    <Button
        android:id="@+id/callButton"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="Call" />
    <Button
        android:id="@+id/contacts"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="Con" />
    <Button
        android:id="@+id/del"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
            android:text="Del" />
    </LinearLayout>

</LinearLayout>

And MainActivity.java:

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);
    }
}
}*/
    }
}

change your onClicklistener to this :

oneBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // no need to use onBtn here cuz the param v is the clicked View
        Button clickedButton = (Button) v;
        clickedButton.setText("1");
        numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());
        //numTxt.append("1");
    }
});

Edittext's getText() method returns an Editable Object, see :

http://developer.android.com/reference/android/widget/EditText.html#getText () http://developer.android.com/reference/android/text/Editable.html

要获取edittext的文本,必须在getText()之后调用toString() getText()

numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM