2013-03-13 67 views
4

這是XML代碼:點擊按鈕不會改變TextView的價值

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:text="Your total is 0" 
    android:textSize="20dp" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/button1" 
    android:layout_centerVertical="true" 
    android:text="Subtract one" 
    android:textSize="20dp" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="62dp" 
    android:text="Add one" 
    android:textSize="20dp" /> 

這是java代碼:

package com.example.helloworld; 

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

public class MainActivity extends Activity { 

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

int counter; 
Button add, sub; 
TextView display; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    counter = 0; 
    add = (Button) findViewById(R.id.button1); 
    sub = (Button) findViewById(R.id.button2); 
    display = (TextView) findViewById(R.id.textView1); 
    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter++; 

      display.setText("Your total is " + counter); 
     } 
    }); 

    sub.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter--; 

      display.setText("Your total is " + counter); 
     } 
    }); 

    return true; 
} 

} 

當我按下按鈕沒有任何反應的textview文本。

請幫忙。我希望顯示器在添加或減去之後顯示計數器的值,具體取決於按下哪個按鈕。

+0

您是否收到任何崩潰?如果不是,我相信你的onCreateOptionsMenu函數沒有被調用。 – Triode 2013-03-13 06:51:08

+0

看看我的解決方案。 – Nezam 2013-03-13 06:56:49

回答

2

你放置你的代碼錯了地方。你不和菜單操作,你在這兒不需要把你在public boolean onCreateOptionsMenu(Menu menu) {}按鈕不會得到任何動作/火

移動這個

counter = 0; 
    add = (Button) findViewById(R.id.button1); 
    sub = (Button) findViewById(R.id.button2); 
    display = (TextView) findViewById(R.id.textView1); 
    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter++; 

      display.setText("Your total is " + counter); 
     } 
    }); 

    sub.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter--; 

      display.setText("Your total is " + counter); 
     } 
    }); 

到您的onCreate()方法,並嘗試再次

+0

謝謝了。我的第一個應用程序正在工作。 – 2013-03-13 07:32:51

+0

@AshishGarg不客氣。如果這個答案可以幫助你接受並投票。謝謝 – 2013-03-13 07:36:20

0

問題:

了Android documentation本身說,都應該在自己的onCreate使用findViewById

的onCreate(捆綁),在這裏你初始化你的活動。大多數 重要的是,在這裏你通常會調用setContentView(int)和一個定義UI的 佈局資源,並且使用findViewById(int)到 檢索該UI中的窗口小部件,您需要以編程方式與 交互。

但你在CreateOptionsMenu.Perform以下步驟使用它們:

起初使這些聲明類級別:

public class MainActivity extends Activity { 
int counter; 
    Button add, sub; 
TextView display; 
//......more code below 

移動這一段代碼到你onCreate(Bundle savedInstanceState) {而不是onCreateOptionsMenu(Menu menu) {。特別是反碼

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

    //assigning initial value to counter 

counter = 0;//counter set 

//getting the controls 

    add = (Button) findViewById(R.id.button1); 
    sub = (Button) findViewById(R.id.button2); 
    display = (TextView) findViewById(R.id.textView1); 

//setting Listeners 

    add.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     counter++; 

     display.setText("Your total is " + counter); 
    } 
}); 

sub.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     counter--; 

     display.setText("Your total is " + counter); 
    } 
}); 
} 
+0

非常感謝。我的應用程序現在終於工作了。 – 2013-03-13 07:32:13

+0

很高興知道。如果我的答案幫助解決了您的問題,請將其標記爲答案。 – Nezam 2013-03-13 07:33:33

0
public class MainActivity extends Activity { 

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


     counter = 0; 
     add = (Button) findViewById(R.id.button1); 
     sub = (Button) findViewById(R.id.button2); 
     display = (TextView) findViewById(R.id.textView1); 
     add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter++; 

       display.setText("Your total is " + counter); 
      } 
     }); 

     sub.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter--; 

       display.setText("Your total is " + counter); 
      } 
     }); 

    } 

    int counter; 
    Button add, sub; 
    TextView display; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 

     return true; 
    } 
} 
+0

你必須把你的代碼放在oncreate方法而不是onCreateOptionsMenu – duggu 2013-03-13 06:41:14

+0

你能解釋一下嗎?很感興趣的解釋 – Triode 2013-03-13 06:42:43

+0

看看我對問題的解釋.. – Nezam 2013-03-13 06:50:23

0

運行它在onCreate方法做所有的東西,不是在onCreateOptionsMenu提到以下

//下面的代碼

int counter; 
Button add, sub; 
TextView display; 

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

counter = 0; 
add = (Button) findViewById(R.id.button1); 
sub = (Button) findViewById(R.id.button2); 
display = (TextView) findViewById(R.id.textView1); 
add.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     counter++; 

     display.setText("Your total is " + counter); 
    } 
}); 

sub.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     counter--; 

     display.setText("Your total is " + counter); 
    } 
}); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

return true; 
} 

} 
+0

爲什麼需要在onCreate()方法中調用getMenuInflater()? – shaonAshraf 2013-03-13 06:51:18

+0

謝謝。我的應用程序正在工作。 Yipee。 – 2013-03-13 07:32:29

0

你必須在onCreate方法中完成你的任務。 onCreateOptionsMenu(菜單菜單)方法用於選項菜單中的任務配置。因此,請在onCreate方法中替換您的代碼。 完整的代碼如下。替換它並嘗試一下。希望這將工作:)

 package com.example.helloworld; 

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

     public class MainActivity extends Activity { 

int counter; 
Button add, sub; 
TextView display; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    counter = 0; 
    add = (Button) findViewById(R.id.button1); 
    sub = (Button) findViewById(R.id.button2); 
    display = (TextView) findViewById(R.id.textView1); 
    add.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     counter++; 

     display.setText("Your total is " + counter); 
    } 
}); 

sub.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     counter--; 

     display.setText("Your total is " + counter); 
    } 
}); 
} 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 


     return true; 
    } 

    } 

+0

非常感謝。該應用正在使用我的手機。 – 2013-03-13 07:34:18

相關問題