如何在EclipseAndroid中调用整个类
我有一个navigationBtns.java类,它包含导航按钮及其onClick操作的代码。这就是它的全部功能,导航如何在EclipseAndroid中调用整个类,android,eclipse,Android,Eclipse,我有一个navigationBtns.java类,它包含导航按钮及其onClick操作的代码。这就是它的全部功能,导航 package Mobile.Smart; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.
package Mobile.Smart;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class navigationBtns extends Activity {
public static ImageButton vocabButton;
public static ImageButton shapesButton;
public static ImageButton mathButton;
public static ImageButton mainNextButton;
public static Intent vocabulary;
public static Intent shapes;
public static Intent math;
public static Intent mainNext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
/*Navigation buttons*/
vocabButton = (ImageButton)findViewById(R.id.nextScreenWords);
shapesButton = (ImageButton) findViewById(R.id.nextScreenShapes);
mathButton = (ImageButton) findViewById(R.id.nextScreenMath);
mainNextButton = (ImageButton) findViewById(R.id.goHome);
vocabulary = new Intent(this,screen1.class);
shapes = new Intent(this,screen2.class);
math = new Intent(this,screen3.class);
mainNext = new Intent(this,MobileSmartKidsActivity.class);
vocabButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(vocabulary);
}});
shapesButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
startActivity(shapes);
}});
mathButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
startActivity(math);
}});
mainNextButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
startActivity(mainNext);
}});
}
}
在同一个软件包中,我有MobileSmartKidsActivity,它播放视频,然后具有navigationBtns.java中指定的相同导航按钮。还有3个其他文件使用相同的按钮。我已经重写了所有单独java文件中所有按钮的代码,这是过度冗余和内存消耗。如何在MobileSmartKidsActivity类中调用navigationBtns类?我以前在Java3D中做过,但我不知道如何在Android中做
包移动。智能
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;
import Mobile.Smart.navigationBtns;
public class MobileSmartKidsActivity extends Activity {
private VideoView vd;
private ImageButton vocabularyButton;
private ImageButton shapesButton;
private ImageButton mathButton;
private ImageButton mainNextButton;
private Intent vocabulary;
private Intent shapes;
private Intent math;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
/*load and play video on home page*/
vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.nursery);
MediaController mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
vocabularyButton = (ImageButton)findViewById(R.id.nextScreenWords);
shapesButton = (ImageButton) findViewById(R.id.nextScreenShapes);
mathButton = (ImageButton) findViewById(R.id.nextScreenMath);
mainNextButton = (ImageButton) findViewById(R.id.goHome);
vocabulary = new Intent(this,screen1.class);
shapes = new Intent(this,screen2.class);
math = new Intent(this,screen3.class);
vocabularyButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
startActivity(vocabulary);
}});
shapesButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
startActivity(shapes);
}});
mathButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
startActivity(math);
}});
mainNextButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{}});
}
}
您可以创建一个ButtonUtil类,该类具有一个静态方法,该方法将导航按钮作为参数并初始化它们。现在您只需要在两个onCreate方法中调用该方法。您可以将按钮部分实现为一个,然后将其添加到所有需要的活动中。这样按钮的代码将只在一个地方
注意:片段需要API级别11或更高。只需将nav按钮代码放入活动类中,就像您所拥有的那样,然后在创建所有其他活动时从该类而不是活动进行扩展。我会尝试一下,但我在这方面还是个新手,所以我必须读一读。谢谢,我试过了。应用程序运行,但按钮没有响应。这就是navigationBtn类现在的样子: