displaying a string on the textview when clicking a button in android

后端 未结 9 2005
情深已故
情深已故 2020-12-10 08:43

I\'m very new to Android development and just started to study.

What I\'m trying is to add a button and when that button is pressed a text \"my first project\" to ge

9条回答
  •  独厮守ぢ
    2020-12-10 09:14

    You can do like this:

     String hello;
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);
        super.onCreate(savedInstanceState);
    
        mybtn = (Button)findViewById(R.id.mybtn);
        txtView=(TextView)findViewById(R.id.txtView);
        txtwidth = (TextView)findViewById(R.id.viewwidth);
        hello="This is my first project";
    
    
        mybtn.setOnClickListener(this);
    }
    public void onClick(View view){
        txtView.setText(hello);
    }
    

    Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file. Hope this will help you.

提交回复
热议问题