2013-07-16 62 views
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; 
} 

}

+0

是的,這是因爲您爲第一個按鈕的點擊操作中的第二個按鈕設置了點擊偵聽器。由它的lloks,它似乎是故意的。 – njzk2

+0

您需要在其他一些事件中設置onclicklisteners,通常以/ windows加載或創建事件的形式完成。 –

+1

我明白了,我知道它的順序我的代碼多數民衆贊成在錯誤,我試圖讓我的頭周圍應該如何訂購 – AndroidCB

回答

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

啊是的,這是有效的,非常感謝您的幫助!我現在知道未來該怎麼做 – AndroidCB

0

您需要的的onCreate()superfunction內設置你所有的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; 
} 
相關問題