Java 安卓:OnClick按钮不';t向文本视图追加/删除值

Java 安卓:OnClick按钮不';t向文本视图追加/删除值,java,android,mobile,android-intent,android-activity,Java,Android,Mobile,Android Intent,Android Activity,我根本无法让AndroidonClick事件工作。我在activity_main.xml中呈现了一个文本视图和一些按钮。在MainActivity.java中,我尝试将onClick事件附加到所有将其值附加到输入字段的按钮上。然而,我很难找到一些关于使拨号器工作的教程、书籍等。我有三本书,没有一本是关于使用通话功能或拨号的。我在网上找到的教程都是关于向用户展示如何为拨号程序设置根目录和主题的,而不是如何为拨号程序编程的。因此,我的问题是: 1) 在文本视图中添加值会出现什么问题? 2) 如何使用

我根本无法让Android
onClick
事件工作。我在activity_main.xml中呈现了一个文本视图和一些按钮。在MainActivity.java中,我尝试将
onClick
事件附加到所有将其值附加到输入字段的按钮上。然而,我很难找到一些关于使拨号器工作的教程、书籍等。我有三本书,没有一本是关于使用通话功能或拨号的。我在网上找到的教程都是关于向用户展示如何为拨号程序设置根目录和主题的,而不是如何为拨号程序编程的。因此,我的问题是:

1) 在文本视图中添加值会出现什么问题? 2) 如何使用del onClick事件删除最后一个值? 3) 当用户单击文本视图时,如何阻止它向上拉键盘?即,如何使其禁用/只读(可能),以便只有按钮才能更新

谢谢你的帮助。在下面的代码中,我将按钮减少到一个。我所做的就是复制所有的按钮并更改ID、文本等。所以所有的方法都是相同的,但我不能只让一个按钮工作。同样,对于下面的代码,请不要对我笑得太厉害

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>

将onClicklistener更改为:

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的getText()方法返回可编辑对象,请参见:

()

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


这对我不管用。单击一个按钮仍然没有任何作用。您是否仍然缺少“new View.OnClickListener(){”下的@Override?这可能是罪魁祸首,具体取决于您的编译设置。如何检查我的编译设置?或者如何检查我正在使用的API?在eclipse中,右键单击项目>属性>Java编译器>配置工作区设置(或启用特定于项目的设置)>“编译器符合性级别”应该是1.6行。我会试试。我刚从使用实际设备的人那里得到消息,他们能够很好地打开它。所以我正在研究这是否是一个模拟器问题。我现在倾向于这样做,因为我看到的每一篇文章都显示了您的确切代码。所以我知道您的权利,但两者都有冲突在我的代码或编译器或模拟器的其他地方…但是+1是正常的,不管你对我的第一个问题有正确的答案,我只是在其他地方搞砸了。这对我也不起作用。单击一个按钮仍然没有任何作用。+1对你也一样。这对我不起作用,但对我的第一个问题来说是正确的答案斯蒂恩和我知道问题在别处,我必须找到它们。
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");
    }
});
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());