本文介绍了onActivityResult似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MainActivity.java - 主类
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView ClockButton;
private LinearLayout linearLayout;
private TextView[] textViews;
private int hours;
private int minutes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViews=new TextView[20];
linearLayout=(LinearLayout)findViewById(R.id.LinearLayout1);
ClockButton=(ImageView)findViewById(R.id.ClockIconButton);
ClockButton.setOnClickListener(this);
}
public void onChangeContentView()
{
Intent intent = new Intent(this, MainActivity2.class);
this.startActivityForResult(intent,1);
}
@Override
public void onClick(View v) {
onChangeContentView();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_SHORT).show();//test
hours=Integer.parseInt(data.getStringExtra("H"));
minutes=Integer.parseInt(data.getStringExtra("M"));
for(int i=0;i<textViews.length;i++)
{
if(textViews[i]==null)
{
textViews[i].setText(hours+" : "+minutes);
linearLayout.addView(textViews[i]);
break;
}
}
}
}
MainActivity2.java - 子类
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private ImageView OkButton;
private TimePicker timePicker;
private int hours;
private int minutes;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
OkButton = (ImageView) findViewById(R.id.OkIcon);
OkButton.setOnClickListener(this);
timePicker = (TimePicker) findViewById(R.id.TimePicker1);
}
@Override
public void onClick(View v) {
hours = timePicker.getCurrentHour();
minutes = timePicker.getCurrentMinute();
Intent intent = new Intent();
intent.putExtra("H", hours);
intent.putExtra("M", minutes);
setResult(1, intent);
finish();
}
}
我是试图解决它。这没问题,但我看不到在我的Main类中 onActivityResult()
中调用的 Toast
。 ( Toast.makeText(getApplicationContext(),test,Toast.LENGTH_SHORT)。show();
)
I've tried to fix it. It's no problem, but I can't see the the Toast
which is called in onActivityResult()
in my Main class. (Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_SHORT).show();
)
我错过了什么?
推荐答案
int RESULT_OK
标准活动结果:操作成功。
int RESULT_OKStandard activity result: operation succeeded.
常数值:-1(0xffffffff)
Constant Value: -1 (0xffffffff)
这样做:
setResult(RESULT_OK);
这篇关于onActivityResult似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!