In my previous article I discussed about how to create user interfaces in Android. Here I’m going to show you how you can add event handler to that UI. To do that, first add these two lines between package and class declaration.
To do that, first add these two lines between package and class declaration.
import android.view.View; import android.widget.Button;
Now replace your onCreate method using following code.
import android.view.View;
import android.widget.Button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testview);
Button ok = (Button)findViewById(R.id.btn_testview_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
moveToMain();
}
});
}
private void moveToMain() {
// TODO Auto-generated method stub
setContentView(R.layout.main);
}
In the above code "btn_testview_ok" is the id of the "Ok" button in previous article. When you click that button, the moveToMain(); method will be called and content view will changed as "main.xml". I will discuss more about this topic in my next posts.


