2 slaks SLaks 于 2013.07.31 14:10 提问

点击按钮不能改变textview的值

XML 代码:

<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的 text一点也没变化。我想根据按钮的点击增加或减少,来显示计算器的值。如何实现?

3个回答

Jave_
Jave_   2013.07.31 14:46

你的代码比较乱,你如果是通过menu去触发弹出menu菜单,则应该重写下面这个方法去判断是add还是stub按钮,进行操作:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch(item.getItemId())//得到被点击的item的itemId
        {
        case R.id.button1://这里的Id就是布局文件中定义的Id,在用R.id.XXX的方法获取出来
            break;
        case R.id.button2:
            break;
        }
        return true;
    }

如果你的按钮时activity_main布局里的按钮,则应该按你这种方式去写

cj6585256
cj6585256   2013.07.31 16:13

如果你的需求不是点击menu再触发的话
你的代码应该写在onCreate中

@Override
    public boolean onCreate(Bundle saveInstance) {
    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;
}

}

wurenbi45
wurenbi45   2013.08.02 17:30

counter = 0;放到类里面

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!