How to connect two layout java android?
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying to do this thing: I have one layout named "login.xml" and other "main.xml". I want first load "login.xml", input login name and then if login name correct load "main.xml".
public class simpleprogram extends Activity {
/** Ca开发者_JAVA技巧lled when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
How to call main.xml correctly?
You don't need to change the layout. You should create a separate activity with content view == R.layout.main(similarly to how you have done with simpleprogram
activity) and start the activity in the moment of successfull login.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button loginBtn = (Button) findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// check that login is correct
// if so...
Intent intent = new Intent(simpleprogram.this, SecondActivity.class);
startActivity(intent);
}
}
}
Refer to the original developers guide.
Ofcourse you could also just set a new contentview after commit?
public class simpleprogram extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(checkForLoggedIn()){
setContentView(R.layout.main);
}else{
setContentView(R.layout.login);
}
}
Or do something similar with an onClick
you have to type :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button loginBtn = (Button) findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// check that login is correct
@Override
public void onClick(View args0) {
setContentView(R.layout.main);
}
}
}
}
精彩评论