显示 Toast 消息
Displaying Toast message
我想从 class 生成一个 Toast,但没有从中扩展视图。
我的程序中有 3 个 classes。它们是:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent screenIntent = new Intent(this, Screen.class);
this.startActivity(screenIntent);
StartVoiceRecognition StartVoiceRecognitionChild = new StartVoiceRecognition();
StartVoiceRecognitionChild.makeToast();
}
}
,
public class Screen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen);
//species field
tvSpecies = this.findViewById(R.id.textboxSpeciesxml);
tvSpecies.setText(MainActivity.szSpecies);
...
}
}
和
public class StartVoiceRecognition extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void makeToast(Context context) {
Toast.makeText(getApplicationContext(), "toast content here", Toast.LENGTH_SHORT).show();
}
}
StartVoiceRecognition class 中的 Toast 导致 Screen class 中的布局被破坏。如何在 StartVoiceRecognition 中加入 Toast,这样就不会发生这种情况?
将此方法放在另一个 class 中。一般我们把这些常用的方法放在一个叫Utils的包里,你想用哪里就用什么。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
StartVoiceRecognition.makeToast(this, "This is a Toast msg.");
}
}
和
public class StartVoiceRecognition extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void makeToast(Context mContext,String message){
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
}
我想从 class 生成一个 Toast,但没有从中扩展视图。
我的程序中有 3 个 classes。它们是:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent screenIntent = new Intent(this, Screen.class);
this.startActivity(screenIntent);
StartVoiceRecognition StartVoiceRecognitionChild = new StartVoiceRecognition();
StartVoiceRecognitionChild.makeToast();
}
}
,
public class Screen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen);
//species field
tvSpecies = this.findViewById(R.id.textboxSpeciesxml);
tvSpecies.setText(MainActivity.szSpecies);
...
}
}
和
public class StartVoiceRecognition extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void makeToast(Context context) {
Toast.makeText(getApplicationContext(), "toast content here", Toast.LENGTH_SHORT).show();
}
}
StartVoiceRecognition class 中的 Toast 导致 Screen class 中的布局被破坏。如何在 StartVoiceRecognition 中加入 Toast,这样就不会发生这种情况?
将此方法放在另一个 class 中。一般我们把这些常用的方法放在一个叫Utils的包里,你想用哪里就用什么。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
StartVoiceRecognition.makeToast(this, "This is a Toast msg.");
}
}
和
public class StartVoiceRecognition extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void makeToast(Context mContext,String message){
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
}