【问题标题】:Android - checked perform actionAndroid - 选中执行操作
【发布时间】:2014-02-06 05:23:13
【问题描述】:

由于我是 android 新手,我的问题是我正在开发一个应用程序,我想通过单击主页中的按钮来增加/减少数字,但首先应该在设置菜单中检查复选框被选中,如果选中则仅执行操作...

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>

【问题讨论】:

    标签: android checkbox sharedpreferences


    【解决方案1】:

    执行此操作,在您的按钮单击事件代码中:

     @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);
    }
    

    //把这个放到主Activity中

     @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
                }
            }
        }
    

    【讨论】:

    • 检查您的复选框 id
    • 尝试为它设置监听器并定义一个布尔变量并检查它是否为真然后做你的工作。请参阅此处的监听器示例。mkyong.com/android/android-checkbox-example
    • 我什至看过这个例子,它与我不匹配..'
    • 你的复选框在不同的活动中,按钮在不同的活动中,对吧?
    • yaa.. 那是正确的.. 现在我该怎么办,你能帮我吗?我昨天一直在挣扎..
    猜你喜欢
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2018-05-30
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多