MARS老师安卓开发学习_view

来源:互联网 发布:网络剧发行方式 编辑:程序博客网 时间:2024/06/10 15:13

学习使用代码获取控件并用代码对其操作    监听器的使用


使用监听器使得每次按一下按钮就可以使text的数字加1


package com.fb.firstprj;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
private TextView textView;
private Button button;
int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.textView1);//获取对象
        button = (Button)findViewById(R.id.button);
        textView.setText("Hello Worl");
        textView.setBackgroundColor(Color.BLUE);
        ButtonListener buttonListener = new ButtonListener();//创建一个监听器对象
        button.setOnClickListener(buttonListener);//绑定监听器对象
    }




    @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;
    }
    
    class ButtonListener implements OnClickListener{//新建一个监听器对象


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count++;//每点击一次按钮就加一
textView.setText(count + "");//通过加上一个字符串将int编程字符串
}
   
    }
    
}