using a generic class in android
Hi i am new to android.. i have the following code..
package squash.trainer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class SquashTrainerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Button btn_start = (Button) findViewById(R.id.btn_start);
//btn_start.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
// setContentView(R.layout.selecttopmenu);
// }});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater. inflate(R.menu.mainmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item. getItemId() ) {
case R. id.btn1:
BugsSequence();
return true;
case R. id.btn2:
return true;
case R. id.btn3:
return true;
case R. id.btn4:
return true;
default:
return super. onOptionsItemSelected(item) ;
}
}
private void BugsSequence() {
// TODO Auto-generated method stub
}
}
for each case i want to use the a class to load the new layout and logic...
currently i have a test class called Bugssequence
package squash.trainer;
import android.os.Bundle;
import android.app.Activity;
//import android.view.View;
//import android.widget.Button;
public class BugsSequence extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
setContentView(R.layout.selecttopmenu);
}
/**
* @param args
*/
public static void main(String[] args) {
//
}
}
How can i get it load the new layout.. when i press on button 1.. it does nothing.. or is there a better way to load 4 different layouts /class(logic) one for each of the menus. Should i开发者_如何学JAVA even be creating new activities for each screen / layout ?
Thanks you for your assistance...
Complete guide can be found here: http://developer.android.com/guide/topics/fundamentals/activities.html
At first, your activity should be declared in the manifest
<activity android:name=".YourActivity"
android:label="@string/youractivitylabel">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
and then you can start YourActivity from code:
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
You can put easily some extra data into the intent and/or start it for a result
startActivityForResult(intent, PICK_CONTACT_REQUEST);
Read the link above, it's very useful!
精彩评论