Android | StartActivityForResult不会做任何事情
当我在一个新的活动中写一个国家的时候,我有家庭作业来做这件事(forresult) 它将把它写在主活动和我写的颜色中,新活动将把文本颜色改为我写的颜色。 但是“确定”和“取消”按钮不会传递搜索到的任何信息,因为没有成功 主要活动-Android | StartActivityForResult不会做任何事情,android,android-intent,start-activity,Android,Android Intent,Start Activity,当我在一个新的活动中写一个国家的时候,我有家庭作业来做这件事(forresult) 它将把它写在主活动和我写的颜色中,新活动将把文本颜色改为我写的颜色。 但是“确定”和“取消”按钮不会传递搜索到的任何信息,因为没有成功 主要活动- public class MainActivity extends Activity implements OnClickListener { TextView tvDisplayCountry; Random crazy; @Override protected
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);
}
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;
}
}
}
乡村活动-
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);
}
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\u OK
或RESULT\u cancelled
,而不是12
)
只需将
开关(resultCode)
更改为开关(requestCode)
您在LogCat中是否遇到任何错误?请在代码中添加一些Log.i(“,“有用的东西”),并检查哪里有问题。我们无法测试您的全部代码!在日志猫没有问题推或取消,主要活动打开没有变化你也应该考虑遵循。“Color”和“Country”变量是大写的,通常为类和接口保留。变量名应为小写。Android(和Java AWT)有一个Color类,您的代码看起来像是在试图访问该类的静态equalsIgnoreCase()方法。