我不知道您是否可以从非活动类(不是活动本身的类)启动祝酒词。我想在我的输入过滤器类中使用我的 toast,所以我首先尝试将整个活动传递给另一个类,但严重失败。所以我决定将上下文本身传递给非活动类,在本例中是 InputFilterMinMax。从那里我使用该上下文尝试将 Toast 显示到当前活动。我的问题是,这是否可能,如果是这样,为什么我的 toast 尝试启动后我的应用程序会崩溃。谢谢。
package com.TechDigy.parabolicmotion;
import android.app.Activity;
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.Toast;
public class InputFilterMinMax implements InputFilter {
private int min, max;
Context context;
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}
public InputFilterMinMax(Context context2) {
// TODO Auto-generated constructor stub
context = context2;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int input = Integer.parseInt(dest.toString() + source.toString());
if (!isInRange(min, max, input))
Toast.makeText(context, "Max Angle 90 Degrees", Toast.LENGTH_LONG).show();
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
输入信息活动
package com.TechDigy.parabolicmotion;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EnterInformation extends Activity {
//Name the String which will be used to identify the file name of the SharedPreference
public static final String REFS = "checkedornot";
//final String[] names = new String[]{"DegreeLaunchInitialVelocity","DegreeLaunchInitialYVelocity","DegreeLaunchInitialXVelocity","InitialYVelocityInitialXVelocity"};
boolean DegreeLaunchInitialVelocity,DegreeLaunchInitialYVelocity,DegreeLaunchInitialXVelocity,InitialYVelocityInitialXVelocity;
boolean testout = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
//Reference the Edit Texts
final EditText InitialVelocity = (EditText) findViewById(R.id.eTInitialVelcity);
final EditText DegreeOfLaunch = (EditText) findViewById(R.id.eTDegreeOfLaunch);
final EditText InitialYVelocity = (EditText) findViewById(R.id.eTInitialYVelocity);
EditText TimeOfFlight = (EditText) findViewById(R.id.eTTimeOfFlight);
EditText DistanceXDirection = (EditText) findViewById(R.id.eTDistanceTraveled);
EditText PeakHeight = (EditText) findViewById(R.id.eTPeakHeight);
final EditText InitialXVelocity = (EditText) findViewById(R.id.eTInitialXVelcity);
// if(DegreeOfLaunch.getText().toString().length() >2){
// Toast.makeText(getApplicationContext(), "Max Angle 90 Degrees", Toast.LENGTH_LONG).show();
// }
//Pass the context
InputFilterMinMax context = new InputFilterMinMax(getApplicationContext());