开发者

Need help getting buttons to work

I am trying to get my first button to update a display number in my view when clicked. This view will have several buttons and "outputs" displayed. After reading examples and Q's here, I finally put something together that runs, but my first button is still not working;

   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));
    }

The .xml for this button is;

     <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:lay开发者_StackOverflow社区out_toRightOf="@+id/engeq"
    android:textColor="#DD00ff00"
    android:textSize="24sp"/>       

This is within a Relative Layout that appears otherwise OK. When I had set the view to have a TextView with the number set as a string , the number displayed, but I could not figure out how to update the text with a numerical field. That may be my real problem.

I have gone through many examples generally referenced from the dev. site (UI, Common Tasks, various samples), and I am still not seeing the connection here...

Again, this is simply a try at getting variables to respond to buttons and update on the view.

So, a few Q's for anyone that can help;

1) Is there any easier way of doing this (ie. send numerical value to View) ? 2) Why isn't my TextSwitcher displaying the number? 3) Should I be using a TextSwitcher here? 4) Any examples of this you can point me to?

UPDATE!!!: Buttons with feedback are now working. Here is my fix, based on feedback, research and luck;

public class Myview extends Myapp implements 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

}

Some of this appears unnecessary at the moment (ie. Gravity on makeView), but it works! And should work for the 10-20 buttons I want to do misc things with.

The key was getting listeners right, and the whole set-up for TextSwitcher was abit awkward...still learning.

Any other final comments welcomed!

UPDATE UPDATE!! I found this also;

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


Your TextSwitcher isn't displaying the numbers because you need to set the OnClickListener of your buttons in your onCreate() method. Currently your onClick() method is never getting called.

Add this to your onCreate:

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

And make sure your class implements View.OnClickListener

As far as their being an easier way to show a numeric value in a TextView the way you are doing it is fine. You have to pass a String to setText(), and the way you are converting the number to a string is fine.


I would need to see more of your code to see exactly what is going wrong but here is the api demo from the android developer site. My guess is that you need to implement ViewSwitcher ViewFactory and View OnClickListener. I see that you have on click but the modifier doesn't match and I don't see a make view method in your code.

 
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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜