简体   繁体   English

Android按钮单击不起作用,并使用它编辑文本框

[英]Android button click doesn't work and editing textbox with it

I'm really new in coding for Android, just started to learn. 我真的是Android编码的新手,刚刚开始学习。 I have some image buttons to change values displayed in textboxes and I don't know how to do this. 我有一些图像按钮可以更改文本框中显示的值,但我不知道该怎么做。 I tried some tests for buttons but I have some errors which I can't repair. 我尝试了一些按钮测试,但有一些无法修复的错误。

Message from eclipse is: 来自eclipse的消息是:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){}) 视图类型中的方法setOnClickListener(View.OnClickListener)不适用于自变量(新的OnClickListener(){})

and

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){}) 视图类型中的方法setOnClickListener(View.OnClickListener)不适用于自变量(新的OnClickListener(){})

for setOnClickListener and OnClickListener 用于setOnClickListener和OnClickListener

I looked at this thread but still I don't know what to do 我看着这个线程,但我仍然不知道该怎么办

setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this); View类型的setOnClickListener(View.OnClickListener)不适用于参数(SequencerActivity)back.setOnClickListener(this);

Except that, I want to change the Medium text fields with the buttons. 除此之外,我想使用按钮更改“中”文本字段。 Perhaps by the declared integers. 也许由声明的整数组成。

Code: 码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
int vs,vh,ws,wh,wth,wts;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1=(Button) findViewById(R.id.imageButton1);
    b1.setOnClickListener(new OnClickListener()
    {
    protected void onClick(View v)
    {
        Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
    }
});
}

xml XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#012e53"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context="pl.tmalachowski.truewindcalc.MainActivity" >

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="26dp"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:textColor="#bdc3c7" >

                    <TextView
                        android:id="@+id/textView9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/ves"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/podst" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center" >

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/rel"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/podst" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="62dp"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/ves1"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/podst" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="77dp"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/ves2"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/podst" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center" >

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="80dp"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/rel1"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/podst" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center" >

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/rel2"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@color/podst" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <ImageButton
                        android:id="@+id/imageButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@android:drawable/arrow_up_float" />
                </LinearLayout>

Declare like this ImageButton b1; 像这样声明ImageButton b1; instead of Button b1; 代替Button b1; then change to like this following code 然后更改为像下面的代码

b1=(ImageButton) findViewById(R.id.imageButton1);
b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(ApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
        }
    });

I think you are using your onClickListener wrong. 我认为您使用的onClickListener错误。

b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Om nom nom", Toast.LENGTH_SHORT).show();
        }
    });

Please try this usage. 请尝试这种用法。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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