As we have main.xml file for view and another file java in src folder which contains the events, i have made another view, that onclicking a button in main.xml it will open the other view xml which should have a java backend file, when i create my custom view xml, then its backened file doesnot create, and how can i click a button and open a new view with another form, just like we do in web pages and how can our custom view have a java backend file.
Thanks Atif
XML FILES
firstXML.xml
secondXML.xml
FILES
firstActivity.java
public class firstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstXML);
}
}
secondActivity.java
public class secondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondXML);
}
}
Now if you have Button in firstXML
. You want this Button to launch secondActivity:
firstActivity.java
public class firstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstXML);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Some extra notes: Now if you want firstActivity
to send some info to secondActivity
Change
Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
startActivityForResult(myIntent, 0);
to
Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
Bundle muBundle = new Bundle(); //create a Bundle
myBundle.putString("username","Sherif");
myBundle.putInt("userid",1234);
// EXAMINE THE [Bundle Class][2]
myIntent.putExtras(myBundle); //PUT THE Bundle you created in the Intent
startActivityForResult(myIntent, 0);
To capture this data in the secondActivity
public class secondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondXML);
Bundle myBundle = this.getIntent().getExtras(); //You got the bundle
//TO USE THE BUNDLE
String A = myBundle.getString("username"); // A = "Sherif"
int B = myBundle.getInt("userid"); // B = 1234
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.