我有1个有3个不可见文本视图的活动1和一个用3个编辑文本和一个提交按钮打开活动2的按钮。 当按下活动2中的提交按钮时,如何使文本视图可见并且按钮在活动1中不可见?
我正在使用startActivityForResult启动活动2,当按下活动2上的提交按钮时,它将返回到活动1。
活动代码1
public class MainActivity extends AppCompatActivity {
private static final int ACTIVITY_2_RESULT_CODE = 0;
Button btn1;
// This method opens the rod_activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
btn1 = (Button)findViewById(R.id.textView2);
// Capture button clicks
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Start SecondActivity.class for result
Intent myIntent3 = new Intent(MainActivity.this,
SecondActivity.class);
startActivityForResult(myIntent3, ACTIVITY_2_RESULT_CODE);
}
});
}
// This method is called when second activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check that it is the second activity with an OK result
if (requestCode == ACTIVITY_2_RESULT_CODE) {
if (resultCode == RESULT_OK) {
// Get string data from Intent
String string1 = data.getStringExtra("@id/edittext1");
// Set text view with string
TextView textview1 = (TextView)findViewById(R.id.textview1);
textview1.setText(string1);
活动1 xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="4dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:text="@string/name"
android:textSize="18sp"
android:id="@+id/textView1"/>
<Button
android:id="@+id/button1"
android:layout_width="65dp"
android:layout_height="50dp"
android:layout_alignStart="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="0dp"
android:layout_marginTop="16dp"
android:text="@string/name1"
android:textStyle="bold" />
活动代码2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view
setContentView(R.layout.activity_second);
// Create the submit button
btn1 = (Button)findViewById(R.id.bSubmit);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Get the text from EditText and put the string to pass back into an Intent
EditText edittext1 = (EditText)findViewById(R.id.edittext1);
String stringToPassBack = edittext1.getText().toString();
Intent myIntent1 = getIntent();
myIntent1.putExtra("@id/edittext1", stringToPassBack);
活动2 xml
<EditText
android:id="@+id/edittext1"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="02"
android:ems="10"
android:hint="@string/name2"/>
<Button
android:id="@+id/bSubmit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/submit"
android:layout_centerHorizontal="true"/>
答案 0 :(得分:0)
清除活动1中的3个全局字符串变量,并将其中一个变为int
public String Stringvar1="";
public String Stringvar2="";
public String Stringvar3="";
public int intvar = 0;
当按下提交这些字符串变量中的值并设置int = 1
时在活动1的函数Oncreate
textview1.settext(Stringvar1);
textview2.settext(Stringvar2);
textview3.settext(Stringvar3);
以及按钮的可见性
if (intvar == 1) {
button.setvisibality(View.VISIBLE);
}else {
button.setvisibality(View.INVISIBLE);
}
答案 1 :(得分:0)
您可以使用Activity 2 Bundle中保存的变量。我有一个简单的例子来给你一个想法。代码已经过测试。
活动一
public class Main1Activity extends AppCompatActivity {
private TextView one;
private TextView two;
private TextView three;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
one = (TextView)findViewById(R.id.one);
two = (TextView)findViewById(R.id.two);
three = (TextView)findViewById(R.id.three);
one.setVisibility(View.GONE);
two.setVisibility(View.GONE);
three.setVisibility(View.GONE);
Button goToActivity2 = (Button)findViewById(R.id.buttonOne);
goToActivity2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gotoTwoIntent = new Intent(MainActivity1.this, MainActivity2.class);
startActivity(gotoTwoIntent);
}
});
}
@Override
protected void onResume() {
super.onResume();
Bundle bundle = getIntent().getExtras();
if(bundle != null){
String fromTwo = bundle.getString("FROM_TWO");
if(!TextUtils.isEmpty(fromTwo)){
one.setText(bundle.getString("EDITTEXT_VALUE_ONE"));
two.setText(bundle.getString("EDITTEXT_VALUE_TWO"));
three.setText(bundle.getString("EDITTEXT_VALUE_THREE"));
}
}
}
}
活动二
public class Main5Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
final EditText one = (EditText)findViewById(R.id.one);
final EditText two = (EditText)findViewById(R.id.two);
final EditText three = (EditText)findViewById(R.id.three);
Button backToOne = (Button)findViewById(R.id.button_two);
backToOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String valueOne = one.getText().toString();
String valueTwo = two.getText().toString();
String valueThree = three.getText().toString();
Intent backToOne = new Intent(MainActivity2.this, Main5Activity1.class);
backToOne.putExtra("FROM_TWO", "1");
backToOne.putExtra("EDITTEXT_VALUE_ONE", valueOne);
backToOne.putExtra("EDITTEXT_VALUE_ONE", valueTwo);
backToOne.putExtra("EDITTEXT_VALUE_ONE", valueThree);
startActivity(backToOne);
}
});
}
}
答案 2 :(得分:-1)
您正在寻找OnActivityResult。按下活动2中的提交按钮时,使用OnAcitivityResult通知活动1并隐藏按钮。