Button and click display

2019-08-10 17:45发布

I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

public class activity_main extends Activity {

    TextView txtCount;
    Button btnCount;
    int count=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtCount= (TextView) findViewById(R.id.textView);
        txtCount.setText(String.valueOf(count));
        btnCount= (Button)findViewById(R.id.button);

        btnCount.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                count++;
                txtCount.setText(String.valueOf(count));
            }
        });

    }
}

}      

No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.

1条回答
唯我独甜
2楼-- · 2019-08-10 18:21

you onClick() listener is wrong. try this one

btnCount.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                   count++;
                                   txtCount.setText(String.valueOf(count));
                                }
                            });
查看更多
登录 后发表回答