4

这是 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 文本根本没有任何反应。

请帮忙。我希望显示器根据按下的按钮在加减后显示计数器的值。

4

5 回答 5

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() 方法并尝试再次运行它

于 2013-03-13T06:40:47.940 回答
0

问题:

android文档本身说应该findViewById在onCreate本身中使用:

onCreate(Bundle) 是您初始化活动的地方。最重要的是,在这里您通常会使用定义 UI 的布局资源调用 setContentView(int),并使用 findViewById(int) 检索该 UI 中需要以编程方式与之交互的小部件。

但是您在 CreateOptionsMenu 中使用它们。执行以下步骤:

首先使这些声明类级别:

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);
    }
});
}
于 2013-03-13T06:39:34.553 回答
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;
    }
}
于 2013-03-13T06:40:09.063 回答
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;
}

}
于 2013-03-13T06:42:17.663 回答
0

您必须在 onCreate 方法中完成您的任务。onCreateOptionsMenu(Menu menu) 方法用于在选项菜单中设置任务。因此,在 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;
   }

  }

于 2013-03-13T06:47:33.487 回答