2016-04-29 48 views
0

我真的是Android編程的新手,剛開始學習。我有一些圖像按鈕來更改文本框中顯示的值,我不知道如何做到這一點。我嘗試了一些按鈕測試,但我有一些我無法修復的錯誤。從蝕Android按鈕點擊無法正常工作並編輯它的文本框

消息是:

在類型視圖的方法setOnClickListener(View.OnClickListener)是不適用的參數(新OnClickListener(){})

的方法setOnClickListener(View.OnClickListener)在類型視圖不適用的參數(新OnClickListener(){})

爲setOnClickListener和OnClickListener

我看着這個線程,但我仍然不知道該怎麼辦

setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this);

除此之外,我想改變與按鈕中的文本字段。也許是由所聲明的整數。

代碼:

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

<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> 
+0

你在你的XML佈局使用的ImageButton或按鈕發表您的XML佈局 – Masum

回答

0

聲明這樣的ImageButton B1;而不是按鈕b1; 然後更改喜歡這個下面的代碼

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(); 
     } 
    }); 
+0

仍然setOnClickListener(新OnClickListener() –

+0

一樣嗎?我使用ImageButtons – Masum

+0

。現在你可以嘗試 –

0

我認爲您使用的是onClickListener錯誤。

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

請試試這個用法。希望它可以幫助

+0

現在應用程序崩潰跑完之後。 –

+0

什麼是錯誤信息? –