簡體   English   中英

Android Button onClick XML不起作用

[英]Android Button onClick XML not working

Eclipse不喜歡PlusLifeForP1Button的onClick調用的addLifeForP1方法。 它對方法調用說以下內容:-令牌“(”,;上的語法錯誤;預期-令牌“)''上的語法錯誤,; 預期-void是該變量的無效類型。如果您能解決這個問題,請告訴我!

public class ActivityTab1 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content1layout);

        Resources res = getResources();
        ImageButton plusLifeButtonP1 = (ImageButton) findViewById(R.id.PlusLifeForP1);
        TextView lifeViewP1 = (TextView) findViewById(R.id.LifeForP1);

        public void addLifeForP1(View view) {
            CharSequence sequence = lifeViewP1.getText();
            String string = sequence.toString();
            int lifeTotal = Integer.parseInt(string);
            lifeTotal++;
            lifeViewP1.setText(lifeTotal);      
        }
    }
    }

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:label="@+id/LifeForP1"
        android:text="20"
        android:color="#990000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="90px"
        android:layout_y="0px"
        android:textSize="250px"
    />

    <ImageButton
        android:label="@+id/PlusLifeForP1"
        android:background="#ff000000"
        android:src="@drawable/plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="140px"
        android:layout_y="280px"
        android:onClick="addLifeForP1"
    />

    <ImageButton
        android:label="@+id/MinusLifeForP1"
        android:background="#ff000000"
        android:src="@drawable/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="245px"
        android:layout_y="280px"
    />

</AbsoluteLayout>

您缺少onCreate方法末尾的大括號。

將addLifeForP1粘貼到onCreate函數之外。 這樣可以解決問題。

public class ActivityTab1 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedIn`enter code here`stanceState);
        setContentView(R.layout.content1layout);

        Resources res = getResources();
        ImageButton plusLifeButtonP1 = (ImageButton) findViewById(R.id.PlusLifeForP1);
        TextView lifeViewP1 = (TextView) findViewById(R.id.LifeForP1);
    }

    public void addLifeForP1(View view) {
        CharSequence sequence = lifeViewP1.getText();
        String string = sequence.toString();
        int lifeTotal = Integer.parseInt(string);
        lifeTotal++;
        lifeViewP1.setText(lifeTotal);      
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM