在扩展 View 但不扩展 AppCompatActivity 的 class 中使用 startActivityForResult 方法?
Use the startActivityForResult method in a class that extends View and not extends AppCompatActivity?
如何在扩展 View
而不是扩展 AppCompatActivity
的 class 中使用 startActivityForResult
方法?
public class ContentsElement extends View {
public ContentsElement(final Context context, final String text, final int requestCode, FlexboxLayout cl) {
super(context);
final TextView mu = new TextView(getContext());
mu.setText(text);
cl.addView(mu);
mu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(),
mu.getText(), Toast.LENGTH_SHORT);
toast.show();
Context context = getContext();
Intent i = new Intent(context, MainActivity.class);
startActivityForResult(i, requestCode);
}
});
}
}
将 startActivityForResult(i, requestCode);
替换为:
((Activity)context).startActivityForResult(i, requestCode);
如何在扩展 View
而不是扩展 AppCompatActivity
的 class 中使用 startActivityForResult
方法?
public class ContentsElement extends View {
public ContentsElement(final Context context, final String text, final int requestCode, FlexboxLayout cl) {
super(context);
final TextView mu = new TextView(getContext());
mu.setText(text);
cl.addView(mu);
mu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(),
mu.getText(), Toast.LENGTH_SHORT);
toast.show();
Context context = getContext();
Intent i = new Intent(context, MainActivity.class);
startActivityForResult(i, requestCode);
}
});
}
}
将 startActivityForResult(i, requestCode);
替换为:
((Activity)context).startActivityForResult(i, requestCode);