由于我是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>
答案 0 :(得分: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
}
}
}