2014-02-06 65 views
-1

由於我是新來的android,我的問題是我正在開發一個應用程序我想增加/減少數字通過點擊主頁上的按鈕,但首先它應該檢查在設置菜單中的複選框被選中,如果它被選中,那麼只有執行的操作... Settings menuHome pageAndroid - 檢查執行動作

MainActivity.java

package com.example.sanple; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    public Button incre; 
    public Button dec; 
    public TextView nub; 
    public static int count = 0; 
    public String string; 

    /* 
    * @Override protected void onStart() { super.onStart(); 
    * 
    * Bundle bundle = getIntent().getExtras(); if (bundle != null) { if 
    * (bundle.getBoolean("checked")) { MainActivity activity = new 
    * MainActivity(); activity.addListenerOnCheckbox(); } else { 
    * Toast.makeText(getApplicationContext(), "try to check the checkbox", 
    * Toast.LENGTH_SHORT).show(); } 
    * 
    * } } 
    */ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 
     addListenerOnCheckbox(); 

     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    } 

    void addListenerOnCheckbox() { 
     incre = (Button) findViewById(R.id.increment); 
     dec = (Button) findViewById(R.id.decrement); 
     nub = (TextView) findViewById(R.id.brakecounter); 

     incre.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Log.d("src", "button clciked"); 
       count++; 
       string = Integer.toString(count); 
       nub.setText(string); 
      } 
     }); 

     dec.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       Log.d("src", "button clciked"); 
       count--; 
       string = Integer.toString(count); 
       nub.setText(string); 

      } 
     }); 
    } 

    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) { 

     int action = event.getAction(); 
     int keycode = event.getKeyCode(); 
     switch (keycode) { 
     case KeyEvent.KEYCODE_VOLUME_UP: 
      if (action == KeyEvent.ACTION_UP) { 
       int i = count++; 
       if (i > 10) { 
        Toast.makeText(getApplicationContext(), 
          "applying too many times brake", Toast.LENGTH_SHORT) 
          .show(); 

       } 
       string = Integer.toString(count); 
       nub.setText(string); 

      } 
      return true; 
     case KeyEvent.KEYCODE_VOLUME_DOWN: 
      if (action == KeyEvent.ACTION_DOWN) { 
       count--; 
       string = Integer.toString(count); 
       nub.setText(string); 

      } 

     default: 
      break; 
     } 
     return super.dispatchKeyEvent(event); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // MenuInflater inflater = new MenuInflater(getApplicationContext()); 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case R.id.action_settings: 
      Intent startActivity = new Intent(this, Settings.class); 
      startActivity(startActivity); 

      break; 

     default: 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

Settings.java

package com.example.sanple; 

import android.R.color; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.TextView; 

public class Settings extends Activity { 

    public TextView view; 
    public CheckBox box; 
    public Button button; 

    // KeyEvent KeyEvent = null; 

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

     view = (TextView) findViewById(R.id.enable_counter); 
     box = (CheckBox) findViewById(R.id.checkBox1); 
     box.setBackgroundColor(color.black); 
     button = (Button) findViewById(R.id.save); 

     box.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       /*//here we r setting checked true 
       box.setChecked(true); 
       Intent intent = new Intent(); 
       intent.putExtra("Checked", true);*/ 

      } 
     }); 
    } 

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

} 

複選框XML ..

activity_settings.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Settings" > 

    <TextView 
     android:id="@+id/enable_counter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="51dp" 
     android:layout_marginTop="34dp" 
     android:text="enable_counter" 
     android:textColor="#f00" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/enable_counter" 
     android:layout_alignBottom="@+id/enable_counter" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="39dp" /> 

    <Button 
     android:id="@+id/save" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/checkBox1" 
     android:layout_marginBottom="64dp" 
     android:text="save" /> 

</RelativeLayout> 

回答

0

做到這一點,你的按鈕單擊事件代碼:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case R.id.action_settings: 
      Intent startActivity = new Intent(this, Settings.class); 
      startActivityForResult(startActivity); 

      break; 

     default: 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

在設置做到這一點:

public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
    Intent intent = getIntent(); 
    if(isChecked) 
     intent.putExtra("Checked", true); 
    else 
     intent.putExtra("Checked",false); 
      setResult(i,100); 
} 

//把它放在主要活動中

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (resultCode == 100) { 
       boolean val = data.getBooleanExtra("checked",false); 
       if(val==true) 
       //allow to increment decrement 
       else 
       //not 
      } 
     } 
    } 
+0

我做了但沒有工作... –

+0

你有什麼錯誤..? –

+0

像空指針異常.. –