点击不工作

时间:2013-09-10 04:24:39

标签: android android-layout android-intent android-emulator

大家好我正在开发一个应用程序,我在主菜单上设计了一个帮助按钮。我在drawable文件夹中复制了一个自定义的帮助图像文件,并创建了一个Help.xml文件以将其嵌入到布局中。我的功能要求只需单击主菜单上的此帮助按钮,就会弹出包含帮助图像的help.xml。但是当我点击帮助时没有任何反应。


Mainmenu.java

Button.OnClickListener mClickListener = new View.OnClickListener() {

        Animation anim = null;

        @Override
        public void onClick(View v) {
            Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibe.vibrate(60);

            switch (v.getId()) {
            case R.id.BtnDisplay:
                mpool.play(mlogon, 1, 1, 0, 0, 1);
                anim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                anim.setDuration(100);
                mBtn1.startAnimation(anim);
                Intent intent = new Intent(NUGA_MainMenuActivity.this, FileSiganlDisplay.class);
                startActivity(intent);
                //overridePendingTransition(R.anim.zoom_enter,R.anim.zoom_exit);
                overridePendingTransition(R.anim.fade, R.anim.hold);
                break;

            case R.id.BtnSlave:

                mpool.play(mlogon, 1, 1, 0, 0, 1);
                anim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                anim.setDuration(100);
                mBtn2.startAnimation(anim);
                Intent intent1 = new Intent(NUGA_MainMenuActivity.this, BTSmartSlavemodule.class);
                startActivity(intent1);
                //overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
                overridePendingTransition(R.anim.fade, R.anim.hold);

                break ;
            case R.id.takeashot:
                //snapFunction();
                Intent intent2=new Intent(NUGA_MainMenuActivity.this,ImagesActivity.class);
                startActivity(intent2);
                break;

            case R.id.takehelp :
                Intent intent3=new Intent(NUGA_MainMenuActivity.this,HelpActivity.class);
                startActivity(intent3);
                break;

            default:
                break;
            }

        }
    };

-------------------------------------------------------------------------------------------------

Mainmenu.xml

</LinearLayout>

    <Button
        android:id="@+id/BtnSlave"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/mes_mod_linked"
        android:background="@color/btn_red"
        android:textColor="#315683"
        android:textSize="30px" />

    <Button
        android:id="@+id/takeashot"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/History"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
         android:background="@color/btn_purple"
        android:textColor="#315683"
        android:textSize="30px" />

    <Button
        android:id="@+id/takehelp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/Help"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
         android:background="@color/btn_yellow"
        android:textColor="#315683"
        android:textSize="30px" />

</LinearLayout>

help.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mainbackground"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/helppicture" />

</LinearLayout>

HelpActivity

public class HelpActivity extends Activity{

    Button button1 ; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);

    }



}

我不确定我错过了什么....请帮忙

2 个答案:

答案 0 :(得分:1)

这样做

public class Mainmenu extends Activity{

    Button button1 ; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);


       Button BtnSlave = (Button)findViewById(R.id.BtnSlave);
       Button takeashot=(Button)findViewById(R.id.takeashot);
       Button takehelp=(Button)findViewById(R.id.takehelp);

       BtnSlave.setOnClickListener(mClickListener);
       takeashot.setOnClickListener(mClickListener);
       takehelp.setOnClickListener(mClickListener);
    }



}

答案 1 :(得分:0)

onCreate()中,您必须为ClickListener注册Button,如下所示:

    button1=(Button)findViewById(R.id.BtnSlave); 
      button2=(Button)findViewById(R.id.takehelp);  
      button3=(Button)findViewById(R.id.takeashot);  
        button1.setOnClickListener(mClickListener);
       button2.setOnClickListener(mClickListener);
       button3.setOnClickListener(mClickListener);

之后在onCreate()之外写

       OnClickListener mClickListener = new View.OnClickListener() {
              .................
            //your logic.
           switch (v.getId()) {
        case R.id.BtnDisplay:

         break;

        case R.id.BtnSlave:

             break ;
        case R.id.takeashot:
          }