0

我是 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;
}

}

4

2 回答 2

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;
}
于 2013-07-16T08:53:16.190 回答
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;
}
于 2013-07-16T08:52:11.640 回答