简体   繁体   English

OnClick问题,按钮不会设置文本?

[英]OnClick Issue, Button Won't set Text?

Hey guys Im just starting to learn code and just when I think I have something working it doesn't seem to work. 大家好,我刚刚开始学习代码,而当我认为我有工作的时候,它似乎没有任何作用。 My problem is that when I click my button the code I set in the OnClick portion should set the text to the number in the variable I created but nothing changes(ie: It stays at 0) and im wondering why. 我的问题是,当我单击按钮时,我在OnClick部分中设置的代码应将文本设置为我创建的变量中的数字,但没有任何变化(即:它保持为0),我想知道为什么。 Here is my code for this portion: 这是我这部分的代码:

     private static Button button_win;
private static Button button_loss;
TextView winresult;
TextView lossresult;
double win1,lose1,percent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kotal__khan2);
    button_win = (Button) findViewById(R.id.w2);
    button_loss = (Button) findViewById(R.id.L2);
    win1=0;
    winresult= (TextView) findViewById(R.id.Winner);
    lossresult= (TextView) findViewById(R.id.Loser);
    lossresult.setText("0");
    winresult.setText("0");
}
public void OnClickButtonListener(){
    button_win.setOnClickListener(
            new View.OnClickListener (){
                public void onClick(View v){
                    win1= win1+1;
                    winresult.setText(Double.toString(win1));
                }
            }
    );
}

Am I missing something? 我想念什么吗? Thank you in advance. 先感谢您。

You forgot to call OnClickButtonListener(); 您忘记了调用OnClickButtonListener(); in onCreate() . onCreate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM