[英]how I can use object in my file from another file (Android Studio)
Hello I want to call an object(username, name, posts) from Mainactivity.java to Main2Activity.java first layout: The user enter write here name, username and posts and click on the button 您好,我想从Mainactivity.java到Main2Activity.java的第一个布局中调用一个对象(用户名,名称,帖子):用户输入此处写入名称,用户名和帖子,然后单击按钮
second layout: the information the user has given is displayed on the page I want to save the name, username and posts to used in Second layout 第二种布局:用户提供的信息显示在我要保存名称,用户名和帖子的页面上,以便在第二种布局中使用
https://ibb.co/BfsKMgf https://ibb.co/BfsKMgf
public class MainActivity extends AppCompatActivity {
public String username;
public String name;
public String posts;
EditText usernameinput;
EditText nameinput;
EditText postsinput;
Button confirme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameinput = (EditText)findViewById(R.id.username);
nameinput = (EditText)findViewById(R.id.name);
postsinput = (EditText)findViewById(R.id.posts);
confirme = (Button)findViewById(R.id.confirme);
username = usernameinput.getText().toString();
confirme.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
name = nameinput.getText().toString();
posts = postsinput.getText().toString();
Intent otheractivity = new Intent(getApplicationContext(),Main2Activity.class);
startActivity(otheractivity);
finish();
showToast(name);
showToast(username);
showToast(posts);
}
});
}
private void showToast(String text){
Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
}
}
using intent to pass data from one activity to another activity or if you can use fragment then use bundle. 使用意图将数据从一个活动传递到另一个活动,或者如果可以使用片段,则使用捆绑。 follow these tutorials - https://developer.android.com/training/basics/firstapp/starting-activity#java
遵循这些教程-https: //developer.android.com/training/basics/firstapp/starting-activity#java
you have to change the code. 您必须更改代码。
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putStringExtra("key1","editetextvalue")
intent.putStringExtra("key2","editetextvalue2")
startActivity(intent);
and get value in a second activity.
Intent intent = getIntent();
String key = intent.getStringExtra("key1")
please remember if you pass these key exact same get key other not get
Many ways to pass data from one activity to second activity for example using Intent to pass data from one activity to another,another way use sharedpreference to store data and get it anywhere, third store data in static variable 有多种方法可以将数据从一个活动传递到第二个活动,例如使用Intent将数据从一个活动传递到另一个活动,另一种方法是使用sharedpreference存储数据并将其获取到任何地方,第三个将数据存储在静态变量中
using intent to pass data 使用意图传递数据
MainActivity.java MainActivity.java
nameEt = (EditText) findViewById(R.id.name_et);
mobileEt = (EditText) findViewById(R.id.mobile_et);
submitBtn = (Button) findViewById(R.id.submit_btn);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name=nameEt.getText().toString();
mobile_number=mobileEt.getText().toString();
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("name",name);
intent.putExtra("mobile",mobile_number);
startActivity(intent);
}
});
SecondActivity SecondActivity
nameTv = (TextView) findViewById(R.id.name_tv);
mobileTv = (TextView) findViewById(R.id.mobile_tv);
String name,mobile_number;
//get Intent data
name=getIntent().getStringExtra("name");
mobile_number=getIntent().getStringExtra("mobile");
nameTv.setText(name);
mobileTv.setText(mobile_number);
using static variable 使用静态变量
MainActivity.java public static String name; MainActivity.java公共静态字符串名称; public static String mobile_number;
公共静态字符串mobile_number;
nameEt = (EditText) findViewById(R.id.name_et);
mobileEt = (EditText) findViewById(R.id.mobile_et);
submitBtn = (Button) findViewById(R.id.submit_btn);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name=nameEt.getText().toString();
mobile_number=mobileEt.getText().toString();
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}
});
SecondActivity.java SecondActivity.java
nameTv = (TextView) findViewById(R.id.name_tv);
mobileTv = (TextView) findViewById(R.id.mobile_tv);
nameTv.setText(MainActivity.name);
mobileTv .setText(MainActivity.mobile_number);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.