接口回調方式實現點擊button,修改textview的值

public class MainActivity extends Activity {
      private Button testBtn;
  private TextView showText;
  int i=0;
    @Override
     protected void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.activity_main);
	initView();
	initAction();
     }
     public void initView(){
	testBtn=(Button)findViewById(R.id.test_btn);
	showText=(TextView)findViewById(R.id.show_text);
     }

    public void initAction(){
	testBtn.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if (mListener!=null) {
				mListener.onMyClick("第"+i+"次點擊");
				i++;
			}
		}
	});
	setMyChangeListener(new MyButtonClickListener() {
		
		@Override
		public void onMyClick(String testString) {
		showText.setText(testString);
		}
	});
    }
    MyButtonClickListener mListener;

    public void setMyChangeListener(MyButtonClickListener myChangeListener) {
	this.mListener = myChangeListener;
    }

    public MyButtonClickListener getMyChangeListener() {
	return mListener;
    }

    public interface MyButtonClickListener {
	void onMyClick(String testString);
    }
    }
相關文章
相關標籤/搜索