如何在android studio中调用另一个活动方法? [英] How to call another activity method in android studio?
问题描述
我有两个不同的活动.如果用户已登录,则第一个调用菜单(基本),但还有显示用户信息的方法.
I have two different activities. The first calls the menu(base) if the user is logged in, but have also the method for display the user information.
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername;
Button bLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.etUsername);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(this);
userLocalStore = new UserLocalStore(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
}
}
@Override
protected void onStart() {
super.onStart();
if (authenticate() == true) {
startActivity(new Intent(this, Base.class));
}
}
private boolean authenticate() {
if (userLocalStore.getLoggedInUser() == null) {
Intent intent = new Intent(this, Login.class);
startActivity(intent);
return false;
}
return true;
}
public void displayUserDetails() {
User user = userLocalStore.getLoggedInUser();
etUsername.setText(user.username);
etName.setText(user.name);
etAge.setText(user.age + "");
}
}
第二个活动是菜单;此活动有一个名为"bUtente"的按钮,单击该按钮时必须显示用户信息.
The second activity is the menu; this activity has a button called "bUtente" that when clicked must show the user info.
public class Base extends Activity implements View.OnClickListener {
Button bDiario;
Button bUtente;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
bDiario = (Button) findViewById(R.id.bDiario);
bDiario.setOnClickListener(this);
bUtente = (Button) findViewById(R.id.bUtente);
bUtente.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bDiario:
startActivity(new Intent(this, Diary.class));
case R.id.bUtente:
MainActivity prova = new MainActivity();
prova.displayUserDetails();
}
}
}
当我单击"bUtente"按钮时,如何通过执行方法"displayUserDetails()"使我想起其他活动,怎么办?
How can I do that when I click the button "bUtente" reminds me of the other activity by performing the method "displayUserDetails()"?
推荐答案
MainActivity
的布局包含显示用户信息的 View
.如果您位于 Base
中,则该Activity具有不同的布局,该布局没有任何用于显示用户信息的 View
.您可以创建一个显示用户信息的 Dialog
对话框,或者可以向 Base
中添加其他显示用户信息的 View
s,或者您也可以可以重新构造您的应用程序,以便您有另一个 Activity
显示用户信息,并且 MainActivity
和 Base
都可以启动该 Activity 代码>以显示用户信息.
MainActivity
has a layout that contains the View
s that show the user information. If you are in Base
, that Activity has a different layout that doesn't have any View
s for showing the user information. You can create a Dialog
that shows the user information, or you could add additional View
s to Base
that would show the user information, or you could rearchitect your application so that you have another Activity
which shows the user information and both MainActivity
and Base
could start that Activity
to show the user information.
无论如何,您绝对不能使用 new
关键字实例化 Activity
(例如 MainActivity
)!只有Android可以实例化 Activity
组件,因为该框架会在执行操作时设置基础的 Context
.它还跟踪这些组件并管理生命周期回调.
In any case, you absolutely positively cannot instantiate an Activity
like MainActivity
using the new
keyword! Only Android can instantiate Activity
components, because the framework sets up the underlying Context
when it does that. It also keeps track of these components and manages the lifecycle callbacks.
这篇关于如何在android studio中调用另一个活动方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!