按钮一个接一个地工作,而不是同时工作

时间:2013-07-16 08:47:28

标签: java android class button methods

我是java编程的新手,我正在一个活动中创建一个带有多个按钮的简单应用程序。我遇到的问题是按钮只按顺序工作,一个按钮只能在按下另一个按钮后执行其工作,任何帮助都将受到赞赏。 代码如下:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    // Settings button start   
    Button settingsButton = (Button) findViewById(R.id.btnSettings);
    settingsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startSettings();
        }
    });
}
public void startSettings() {
    Intent launchSettings = new Intent(this, SettingsScreen.class);
    startActivity(launchSettings);
    // Settings button end

    // Set A Button start
    Button setAButton = (Button) findViewById(R.id.btnSetA);
    setAButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View view) {
            setzoneA(); 
    }       

});}


    public void setzoneA(){


    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

    dlgAlert.setMessage("Zone Set");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

    dlgAlert.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  //dismiss the dialog  
                }
            });
    // Set A Button end


    // Set B Button start
    Button setBButton = (Button) findViewById(R.id.btnSetB);
    setBButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View view) {
            setzoneB(); 
    }       

});}


    public void setzoneB(){


    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

    dlgAlert.setMessage("Zone Set");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

    dlgAlert.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  //dismiss the dialog  
                }
            });
    }
    // Set B Button end



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:1)

因为你设置了按钮的监听器具有依赖性,所有按钮启动和监听器都应该在onCreate()中。

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    // Settings button start   
    Button settingsButton = (Button) findViewById(R.id.btnSettings);
    settingsButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
          startSettings();
       }
   });
   Button setAButton = (Button) findViewById(R.id.btnSetA);
   setAButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View view) {
         setzoneA(); 
      } 
   });
   // Set B Button start
   Button setBButton = (Button) findViewById(R.id.btnSetB);
   setBButton.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View view) {
          setzoneB(); 
      }  
   });
}

public void startSettings() {
   Intent launchSettings = new Intent(this, SettingsScreen.class);
   startActivity(launchSettings);     
}

public void setzoneA() {

    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    dlgAlert.setMessage("Zone Set");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

    dlgAlert.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              //dismiss the dialog  
            }
        });       
}


public void setzoneB()  {

   AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

   dlgAlert.setMessage("Zone Set");
   dlgAlert.setPositiveButton("OK", null);
   dlgAlert.setCancelable(true);
   dlgAlert.create().show();

   dlgAlert.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              //dismiss the dialog  
            }
        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.main, menu);
   return true;
}

答案 1 :(得分:0)

您需要在onCreate()超级函数中设置所有onClick()侦听器。您当前的解决方案仅在单击上一个按钮时设置侦听器。

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    // Settings button start   
    Button settingsButton = (Button) findViewById(R.id.btnSettings);
    settingsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startSettings();
        }
    });

// Set A Button start
        Button setAButton = (Button) findViewById(R.id.btnSetA);
        setAButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                setzoneA(); 
        }
});


// Set B Button start
        Button setBButton = (Button) findViewById(R.id.btnSetB);
        setBButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                setzoneB(); 
        }       

    });


}
public void startSettings() {
    Intent launchSettings = new Intent(this, SettingsScreen.class);
    startActivity(launchSettings);
    // Settings button end

}


    public void setzoneA(){


    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

    dlgAlert.setMessage("Zone Set");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

    dlgAlert.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  //dismiss the dialog  
                }
            });
    // Set A Button end


    }


    public void setzoneB(){


    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

    dlgAlert.setMessage("Zone Set");
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();

    dlgAlert.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  //dismiss the dialog  
                }
            });
    }
    // Set B Button end



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}