gpt4 book ai didi

安卓| StartActivityForResult 不会做任何事情

转载 作者:行者123 更新时间:2023-11-29 21:52:09 27 4
gpt4 key购买 nike

当我在一个新 Activity 中写一个国家时,我得到了家庭作业来做到这一点(结果)它会把它写在主要 Activity 中,我写的颜色和新 Activity 会将文本颜色更改为我写的颜色。但是“确定”和“取消”按钮不会传输任何搜索到的解决方案未成功的信息

主要 Activity -

public class MainActivity extends Activity implements OnClickListener {
TextView tvDisplayCountry;
Random crazy;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

initsialize();

}

private void initsialize() {
Button bAddCountry = (Button) findViewById(R.id.bAddCountry);
bAddCountry.setOnClickListener(this);
tvDisplayCountry = (TextView) findViewById(R.id.tvDisplayCountry);
// ImageView ivCountryPhoto =
// (ImageView)findViewById(r.id.ivCountryPhoto);
}

@Override
public void onClick(View v) {

switch (v.getId()) {

case R.id.bAddCountry:
Intent countryactivityIntent = new Intent(MainActivity.this,
CountryActivity.class);
startActivityForResult(countryactivityIntent, 12);

break;
}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


switch(resultCode){
case 12:
if (resultCode == Activity.RESULT_OK) {
String Country = data.getStringExtra("country");
String Color = data.getStringExtra("color");
tvDisplayCountry.setText(Country);
if (Color.equalsIgnoreCase("blue")) {
tvDisplayCountry.setTextColor(android.graphics.Color.BLUE);
} else if (Color.equalsIgnoreCase("yellow")) {
tvDisplayCountry
.setTextColor(android.graphics.Color.YELLOW);
} else if (Color.equalsIgnoreCase("gray")) {
tvDisplayCountry.setTextColor(android.graphics.Color.GRAY);
} else if (Color.equalsIgnoreCase("red")) {
tvDisplayCountry.setTextColor(android.graphics.Color.RED);
}
else{
Toast.makeText(MainActivity.this, "canceld", Toast.LENGTH_SHORT).show();
}
break;
}
}

super.onActivityResult(requestCode, resultCode, data);
}

国家 Activity -

public class CountryActivity extends Activity implements OnClickListener {
EditText etCountry, etColor;
String getCountry, getColor;
Button bOk,bCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_country);



init();


}

private void init() {
etColor = (EditText)findViewById(R.id.etColor);
etCountry = (EditText)findViewById(R.id.etCountry);
bOk = (Button)findViewById(R.id.bOk);
bCancel = (Button)findViewById(R.id.bCancel);
bOk.setOnClickListener(this);
bCancel.setOnClickListener(this);


}

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.bOk:
Intent myIntet = new Intent();
getCountry = etCountry.getText().toString();
myIntet.putExtra("country",getCountry );
getColor = etColor.getText().toString();
myIntet.putExtra("color", getColor);

setResult(Activity.RESULT_OK, myIntet);
finish();

break;

case R.id.bCancel:
Intent myIntent = new Intent();

setResult(Activity.RESULT_CANCELED, myIntent);
finish();
break;
}

}

最佳答案

您的 onActivityResult 中有一个错误——您打开了 resultCode(它将是 RESULT_OKRESULT_CANCELED,而不是 12)。

只需将 switch(resultCode) 更改为 switch(requestCode)

关于安卓| StartActivityForResult 不会做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com