Android; Confused by views?
I have created a class (InputControl) which extends the view of my main class (Main), and takes focus of the screen. I have a button on the main xml layout which calls control() and sets up my InputCont开发者_运维百科rol view, from there I capture user input.
How can I return back to the xml layout from the InputControl view class?
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputControl = new InputControl(this);
}
//......SNIP!
public void control(){
setContentView(InputControl);
InputControl.requestFocus();
}
}
public class InputControl extends View implements OnTouchListener {
public InputControl(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
//...I AM CAPTURING USER TOUCH EVENTS HERE
}
}
Unless you have have a specific reason for doing things this way, a better way might be to have a second activity (InputControlActivity for example) with its own layout file and embed your InputControl class into that.
You would then start an instance of the second activity (with startActivity()) - once you're finished in the InputControlActivity, simply pressing the phone's BACK button will close that activity and return to the main one.
精彩评论