当选择子活动中的弹出菜单中的项目时,我尝试更改主要活动的背景颜色。但是,当我运行应用程序并选择所述项目时,应用程序崩溃。这是子活动代码:
public class Modify extends AppCompatActivity {
Button button, submit;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.modify);
button = (Button) findViewById(R.id.button);
submit = (Button) findViewById(R.id.submit);
edit = (EditText) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu pop = new PopupMenu(Modify.this, button);
pop.getMenuInflater().inflate(R.menu.menu,pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
Intent i = new Intent(Modify.this, MainActivity.class);
switch (item.getItemId()){
case R.id.red:
i.putExtra("color","red");
break;
//TODO add more colours
default:
return false;
}
return true;
}
});
pop.show();
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = edit.getText().toString();
Intent intent = new Intent();
intent.putExtra("MESSAGE", message);
setResult(2, intent);
finish();
}
});
}
}
我不确定我在每个案例中声明的意图是否实际上回到了主要活动中的changeBackground方法:
public class MainActivity extends AppCompatActivity {
//GLOBALS
TextView txt;
Button btn;
RelativeLayout rel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.Text);
btn = (Button) findViewById(R.id.Btn);
rel = (RelativeLayout) findViewById(R.id.activity_main);
Intent intent = getIntent();
String color = intent.getStringExtra("color");
changeBackground(color);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//ON CLICK SETS INTENT
Intent intent = new Intent(MainActivity.this, Modify.class);
//INTENT STARTS ACTIVITY 2
startActivityForResult(intent, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
//SET TEXT WHEN RESULT IS ACCEPTED
String message = data.getStringExtra("MESSAGE");
txt.setText(message);
}
}
public void changeBackground(String color) {
if (color.equals("red")) {
rel.setBackgroundColor(Color.RED);
} else if (color.equals("green")) {
rel.setBackgroundColor(Color.GREEN);
} else if (color.equals("blue")) {
rel.setBackgroundColor(Color.BLUE);
}
}
}
答案 0 :(得分:0)
只需使用startActivityforResult(intent);
答案 1 :(得分:0)
试用以下代码:
flatten()
botton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu pop = new PopupMenu(Modify.this, botton);
pop.getMenuInflater().inflate(R.menu.menu,pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
Intent i = new Intent(Modify.this, MainActivity.class);
switch (item.getItemId()){
case R.id.red:
i.putExtra("color","red");
break;
case R.id.magenta:
i.putExtra("color","magenta");
break;
default:
return false;
}
startActivity(i);
finish();
return true;
}
});
pop.show();
}
});
的{{1}}:
onCreate()
希望这有帮助。
答案 2 :(得分:0)
解决了,有点不合理:
子活动:
public class Modify extends AppCompatActivity {
Button button, submit;
EditText edit;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.modify);
button = (Button) findViewById(R.id.button);
submit = (Button) findViewById(R.id.submit);
edit = (EditText) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu pop = new PopupMenu(Modify.this, boton);
pop.getMenuInflater().inflate(R.menu.menu, pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.red:
s="red";
return true;
case R.id.green:
s="green";
return true;
case R.id.blue:
s="blue";
return true;
case R.id.yellow:
s="yellow";
return true;
case R.id.magenta:
s="magenta";
return true;
default:
return false;
}
}
});
pop.show();
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Modify.this,MainActivity.class);
String message = edit.getText().toString();
String colorMessage = s;
i.putExtra("MESSAGE", message);
i.putExtra("COLOR", colorMessage);
setResult(2,i);
finish();
}
});
}
}
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.Text);
btn = (Button) findViewById(R.id.Btn);
rel = (RelativeLayout) findViewById(R.id.activity_main);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Modify.class);
startActivityForResult(intent,2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
txt.setText(message);
String color = data.getStringExtra("COLOR");
changeBackground(color);
}
}
public void changeBackground(String color) {
if (color.equals("red")) {
rel.setBackgroundColor(Color.RED);
} else if (color.equals("green")) {
rel.setBackgroundColor(Color.GREEN);
} else if (color.equals("blue")) {
rel.setBackgroundColor(Color.BLUE);
} else if (color.equals("yellow")) {
rel.setBackgroundColor(Color.YELLOW);
} else if (color.equals("magenta")) {
rel.setBackgroundColor(Color.MAGENTA);
}
}
}
请告诉我是否有更优雅的方式从开关盒中获取颜色,而无需将它们分配给每个开关盒上的变量。
干杯