2010-04-06 77 views
1

我想讓我的第一個按鈕更新我點擊時在視圖中的顯示編號。該視圖將顯示幾個按鈕和「輸出」。在閱讀了這裏的例子和Q之後,我終於把一些東西放在一起,但我的第一個按鈕仍然不起作用。需要幫助讓按鈕工作

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ship_layout); 
     mSwitcher = (TextSwitcher) findViewById(R.id.eng_val); 
     } 

    private TextSwitcher mSwitcher; 

     // Will be connected with the buttons via XML 
     void onClick(View v){ 

      switch (v.getId()) { 
      case R.id.engplus: 
       engcounter++; 
       updateCounter(); 
       break; 
      case R.id.engneg: 
       engcounter--; 
       updateCounter(); 
       break; 
      } 
     } 
    private void updateCounter() { 
     mSwitcher.setText(String.valueOf(engcounter)); 
    } 

該按鈕的.xml是;

 <TextSwitcher 
    android:id="@+id/eng_val" 
    android:visibility="visible" 
    android:paddingTop="9px" 
    android:paddingLeft="50px" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/build" 
    android:layout_toRightOf="@+id/engeq" 
    android:textColor="#DD00ff00" 
    android:textSize="24sp"/>  

這是在相對佈局內出現,否則確定。當我將視圖設置爲使TextView的數字設置爲字符串時,顯示的數字,但我無法弄清楚如何用數字字段更新文本。這可能是我真正的問題。

我已經經歷了許多從開發中引用的例子。網站(用戶界面,常見任務,各種樣本),我仍然沒有看到這裏的連接...

此外,這只是一個嘗試讓變量響應按鈕和更新視圖。

那麼,幾個Q的任何人都可以幫忙;

1)有沒有更簡單的方法來做到這一點(即將數值發送到視圖)? 2)爲什麼我的TextSwitcher不顯示數字? 3)我應該在這裏使用TextSwitcher嗎? 4)你可以指點我的任何例子嗎?

UPDATE !!!:帶反饋的按鈕現在可以正常工作。這是我的修復,基於反饋,研究和運氣;

公共類MyView的擴展MYAPP實現ViewSwitcher.ViewFactory, View.OnClickListener {

public int engcounter = 1; 
private TextSwitcher mSwitcher; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_layout); 
     mSwitcher = (TextSwitcher) findViewById(R.id.eng_val); 
     mSwitcher.setFactory(this); 


    Button engp = (Button) findViewById(R.id.engplus); 
    engp.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view){ 
     engcounter++; 
     updateCounter(); 
     } 
    }); 

    Button engn = (Button) findViewById(R.id.engneg); 
    engn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view){ 
     engcounter--; 
     updateCounter();  
     } 
    }); 

    } 
    private void updateCounter() { 
     mSwitcher.setText(String.valueOf(engcounter)); 
    } 
    public View makeView() { 
     TextView t = new TextView(this); 
     t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
     t.setTextSize(36); 
     return t; 
    } 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

} 

一些這方面出現的時刻(即重力對makeView)是不必要的,但它的作品!並應該爲10-20按鈕工作,我想與其他東西混雜。

關鍵是讓聽衆正確,而TextSwitcher的整個設置是高尚的尷尬......仍在學習。

歡迎任何其他最終意見!

更新更新! 我也發現這一點;

http://developer.android.com/resources/articles/ui-1.6.html

回答

2

您的TextSwitcher沒有顯示數字,因爲您需要在onCreate()方法中設置按鈕的OnClickListener。目前你的onClick()方法永遠不會被調用。

添加到您的onCreate:

Button plus = (Button) findViewById(R.id.engplus); 
plus.setOnClickListener(this); 
Button neg = (Button) findViewById(R.id.engneg); 
neg.setOnClickListener(this); 

並確保你的類就實現View.OnClickListener

它們是在TextView中顯示的數值,你正在做的方式更簡單的方法很好。您必須將一個字符串傳遞給setText(),並且將數字轉換爲字符串的方式沒問題。

+0

嗯......謝謝。我明白你的意思了。我將這些添加到了我的視圖中,現在它崩潰了(在它沒有做任何事情之前,但我可以點擊按鈕)...將繼續查看它... – 2010-04-06 20:39:17

+0

澄清,只有當我點擊這兩個鈕釦。其他按鈕和查看否則是好的。 – 2010-04-06 20:59:45

+0

你應該在你的LogCat輸出中有一個堆棧跟蹤,讓你知道它爲什麼崩潰。在這裏發佈,如果你不明白。 – 2010-04-07 13:17:58

-2

我需要看到更多的代碼,看看到底是怎麼回事錯誤的,但這裏是從android developer site的API演示。我的猜測是你需要實現ViewSwitcher ViewFactory和View OnClickListener。我發現你點擊了,但修飾符不匹配,在代碼中看不到make view方法。


public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory, 
     View.OnClickListener { 

    private TextSwitcher mSwitcher; 

    private int mCounter = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.text_switcher_1); 

     mSwitcher = (TextSwitcher) findViewById(R.id.switcher); 
     mSwitcher.setFactory(this); 

     Animation in = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in); 
     Animation out = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out); 
     mSwitcher.setInAnimation(in); 
     mSwitcher.setOutAnimation(out); 

     Button nextButton = (Button) findViewById(R.id.next); 
     nextButton.setOnClickListener(this); 

     updateCounter(); 
    } 

    public void onClick(View v) { 
     mCounter++; 
     updateCounter(); 
    } 

    private void updateCounter() { 
     mSwitcher.setText(String.valueOf(mCounter)); 
    } 

    public View makeView() { 
     TextView t = new TextView(this); 
     t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
     t.setTextSize(36); 
     return t; 
    } 
}
+0

是的,這部分是我想要的,沒有動畫,並用一個開關來處理每個視圖的10-20個按鈕。 Onclicklistner似乎走的路,工廠似乎沒有幫助....但它仍然無法正常工作...(請參閱我上面的回覆) – 2010-04-06 21:16:03