Tuesday 5 May 2015

Butter kinfe

Main activity
package com.butterknife;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends Activity implements Observer {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
int counttemp = controler_abc.obj.getCounter();

txt.setText(counttemp + "");

}

@InjectView(R.id.textView1)
TextView txt;

@OnClick(R.id.button1)
public void doLogin(Button btn) {
int count = controler_abc.obj.getCounter();
count = count + 1;
controler_abc.obj.setCounter(count);



}

@OnClick(R.id.button2)
public void jump() {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}

@Override
public void update(Observable observable, Object data) {
int counttemp = controler_abc.obj.getCounter();

txt.setText(counttemp + "");


}
}

Main activity 2
package com.butterknife;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity2 extends Activity implements Observer{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ButterKnife.inject(this);
controler_abc.obj.addObserver(this);
//controler_abc.obj.addObserver(this);
int counttemp = controler_abc.obj.getCounter();

txt.setText(counttemp + "");
}
@InjectView(R.id.textView1)
TextView txt;


@OnClick(R.id.button1)
public void doLogin(Button btn) {

int count = controler_abc.obj.getCounter();
count = count + 1;
controler_abc.obj.setCounter(count);

}



@Override
public void update(Observable observable, Object data) {
int counttemp = controler_abc.obj.getCounter();

txt.setText(counttemp + "");
}
}

Controller
package com.butterknife;

public class controler_abc {
public static CounterModel obj=new CounterModel();

}

Counter Model
package com.butterknife;

import java.util.Observable;


public class CounterModel extends Observable{
int counter=0;
public void setCounter(int counter) {
this.counter = counter;

}
public int getCounter() {
return counter;
}


}

No comments:

Post a Comment