2010-04-06 135 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