【问题标题】:buttons work one after the other instead of at the same time [closed]按钮一个接一个地工作,而不是同时工作[关闭]
【发布时间】:2013-07-14 08:43:29
【问题描述】:

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

}

【问题讨论】:

  • 是的,那是因为您在第一个按钮的点击动作中为第二个按钮设置了点击监听器。就其本身而言,这似乎是故意的。
  • 您需要在其他一些事件中设置onclicklisteners,通常在表单/windows加载或创建事件中完成。
  • 我明白了,我知道我的代码顺序是错误的,我正试图弄清楚它应该如何排序

标签: java android class button methods


【解决方案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;
}

【讨论】:

  • 啊,这行得通,非常感谢您的帮助!!我现在就知道以后要做什么了
【解决方案2】:

您需要在 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2014-05-25
    • 1970-01-01
    • 2019-11-24
    • 2012-06-13
    相关资源
    最近更新 更多