我是android studio的初学者。我创建了2个活动,我想从主活动向第二个活动发送一个字符串值,并在第二个活动中显示String。经过许多问题和解决问题(强制关闭),我的应用程序运行良好。但它没有显示(打印)字符串。
我想通过输入重新接收字符串并将其发送到第二个活动并通过setText()打印它;方法
主要活动
package com.ghermez.sibe.salary_usd;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.lang.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1 = findViewById(R.id.button1);
B1.setOnClickListener(new OnClickListener() {
EditText et_tusd = findViewById(R.id.tusd);
String textFromInput_tusd = et_tusd.getText().toString();
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("i1",textFromInput_tusd);
startActivity(i);
}
});
}
}
第二项活动
package com.ghermez.sibe.salary_usd;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.lang.String;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent i1s = getIntent();
TextView textViewTusd = findViewById(R.id.textViewTusd);
String tusd = i1s.getStringExtra("i1");
textViewTusd.setText(tusd);
}
}
LogCat
04-22 18:21:02.486 1690-1717/com.ghermez.sibe.salary_usd E/EGL_emulation: tid 1717: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
04-22 18:21:15.970 1690-1717/com.ghermez.sibe.salary_usd E/EGL_emulation: tid 1717: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
04-22 18:21:17.905 1690-1717/com.ghermez.sibe.salary_usd E/EGL_emulation: tid 1717: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
04-22 18:21:21.557 1690-1717/com.ghermez.sibe.salary_usd E/EGL_emulation: tid 1717: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
04-22 18:21:23.586 1690-1717/com.ghermez.sibe.salary_usd E/EGL_emulation: tid 1717: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
答案 0 :(得分:1)
点击EditText
后,您应该从Button
获取字符串
您在调用onCreate
时初始化变量。
试试这段代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1 = findViewById(R.id.button1);
EditText et_tusd = findViewById(R.id.tusd);
B1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String textFromInput_tusd = et_tusd.getText().toString();
Intent i=new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("i1",textFromInput_tusd);
startActivity(i);
}
});
}
答案 1 :(得分:0)
Done in: 3.1351969242095947
这将textFromInput_tusd指定为编辑文本的当前值。它不会继续更新它。由于您将其放在onCreate中,它将在onCreate中分配一次并且永不更改。请将其放在onClick中,以便在单击按钮时获得最新值
答案 2 :(得分:0)
我在MainActivity中编辑了您的代码,试试这个
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.lang.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button B1 = findViewById(R.id.button1);
EditText et_tusd = findViewById(R.id.tusd);
B1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String textFromInput_tusd = et_tusd.getText().toString();
Intent i=new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("i1",textFromInput_tusd);
startActivity(i);
}
});
}
}
答案 3 :(得分:0)
我做了类似你想要的应用程序。
这是第一个活动:
package com.example.user_pc.abulab;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonClick(View v){
editText = findViewById(R.id.editText);
String username = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, com.example.user_pc.abulab.Next.class);
intent.putExtra("username", username);
startActivity(intent);
}
}
这是第二个活动:
package com.example.user_pc.abulab;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Next extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
String username2 = getIntent().getStringExtra("username");
TextView textView = findViewById(R.id.textView);
textView.setText(username2);
}
}