Hello I'm beginning with android development I created two interfaces, the first a.xml and the sec开发者_运维百科ond b.xml.
In the first I have an ImageButton. My goal is to switch to interface b when the user clicks on the ImageButton (button.setonclicklistner).
How can I use the activity and intent to navigate between the interfaces (I have one file java pg.java the intent function parameters ActivtyA.class. I don't inderstand it (I haven't a file pg.class)
Thank you
The following code is pseudo-code, so don't take it as-is, but adapt it for your needs.
public ActivityA extends Activity implements View.OnClickListener{
onCreate(...){
...
setContentView(R.layout.a);
ImageButton imageButton = (ImageButton)findViewById(R.id.myImageButton);
imageButton.setOnClickListener(this);
}
protected void onClickListener(View v){
Intent switchToActivityBIntent = new Intent(this, ActivityB.class);
startActivity(switchToActivityBIntent);
}
...
}
On ActivityB:
public ActivityB extends Activity{
onCreate(...){
...
setContentView(R.layout.b);
}
}
If you're beginning with Android development, the Android Application Fundamentals are a must.
You may also want to read by blog post about how to add click listeners declaratively to buttons: http://blog.js-development.com/2010/11/android-attaching-clicklisteners.html
I don't understand your question very much.
If you just want to show a new user interface based on b.xml, you could simply call setContentView(R.layout.b)
.
If you want to start another activity whose user interface is based on b.xml; well, generally, in your button.setonclicklistner method, you can do something like this:
Intent intent=new Intent(ActivityA.this, ActivityB.class);
ActivityA.this.startActivity(intent);
Of course, you need to write a class named ActivityB first. You can find more about Intent in the SDK document. Hope it helps.
You need two Activity
classes, ActivityA.java
and ActivityB.java
.
Then, in ActivityA.java
, create the code below in the behaviour that you want to be launched:
Intent myIntent = new Intent(ActivityA.this, ActivityB.class);
ActivityA.this.startActivity(myIntent);
精彩评论