概要
我不清楚如何进行此操作。欢迎任何帮助或问题。
这是我的.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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.cc09june.TeamA"
android:onClick="link">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Team A Offense Selection"
android:id="@+id/TeamAOffSel"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PandR"
android:id="@+id/PandRA"
android:layout_marginTop="32dp"
android:layout_below="@+id/TeamAOffSel"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:onClick="submitPANDR"
android:nestedScrollingEnabled="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MandF"
android:id="@+id/MandFA"
android:layout_below="@+id/PandRA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="submitMANDF" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SandC"
android:id="@+id/SandCA"
android:layout_below="@+id/MandFA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="submitSANDC" />
这是我的这个XML和按钮的JAVA类:
public class TeamAOffense extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_aoffense);
}
// Button for Pick and Roll, where sharedpreferences assigns offense for use in another activity.
public void submitPANDR (View view) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Pick and Roll");
editor.apply();
}
// Button for Move and Flow, where sharedpreferences assigns offense for use in another activity.
public void submitMANDF (View v) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Move and Flow");
editor.apply();
}
// Button for Set and Called, where sharedpreferences assigns offense for use in another activity.
public void submitSANDC (View v) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Set and Called");
editor.apply();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_team_aoffense, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的UPDATED代码,基于建议。谢谢!
public class TeamAOffense extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_aoffense);
Button buttonPandr = (Button)findViewById(R.id.PandRA);
Button buttonMandf = (Button)findViewById(R.id.MandFA);
Button buttonSandc = (Button)findViewById(R.id.SandCA);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
// Calling string for comparison to enable/disable buttons, note these Strings have been set on a previous activity.
String pandrASetting = pref.getString("pandrASetting", null);
String mandfASetting = pref.getString("mandfASetting", null);
String sandcASetting = pref.getString("sandcASetting", null);
if(pandrASetting.equals("OFF") && mandfASetting.equals("OFF") && sandcASetting.equals("OFF")) {
buttonPandr.setEnabled(true);
buttonMandf.setEnabled(true);
buttonSandc.setEnabled(true);
pandrASetting = "ON";
mandfASetting = "ON";
sandcASetting = "ON";
}
if (pandrASetting.equals("OFF")) {
buttonPandr.setEnabled(false);
}
if(mandfASetting.equals("OFF")) {
buttonMandf.setEnabled(false);
}
if (sandcASetting.equals("OFF")) {
buttonSandc.setEnabled(false);
}
}
// Button for Pick and Roll, where sharedpreferences assigns offense for use in another activity.
public void submitPANDR (View view) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Pick and Roll");
editor.putString("pandrASetting", "OFF");
editor.apply();
}
// Button for Move and Flow, where sharedpreferences assigns offense for use in another activity.
public void submitMANDF (View v) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Move and Flow");
editor.putString("mandfASetting", "OFF");
editor.apply();
}
// Button for Set and Called, where sharedpreferences assigns offense for use in another activity.
public void submitSANDC (View v) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Set and Called");
editor.putString("sandcASetting", "OFF");
editor.apply();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_team_aoffense, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
试试这个......
当用户点击按钮时,将按键说1的值与共享偏好中的按键名称一起存储。并获取onTereate activty方法中的值,如果value为1,则禁用该按钮。
public class TeamAOffense extends ActionBarActivity {
Button buttonPandr, buttonSandc, buttonMandf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_aoffense);
buttonPandr = (Button)findViewById(R.id.PandRA);
buttonMandf = (Button)findViewById(R.id.MandFA);
buttonSandc = (Button)findViewById(R.id.SandCA);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
String pandr = pref.getString("PANDR", "0");
String mandf= pref.getString("MANDF", "0");
String sandc = pref.getString("SANDC", "0");
if(pandr.equals("1")){
buttonPandr.setEnabled(false);
}
if(mandf.equals("1")){
buttonMandf.setEnabled(false);
}
if(sandc.equals("1")){
buttonSandc.setEnabled(false);
}
// code to enable the three buttons again, if all the buttons are disabled.
if(mandf.equals("1") && panfr.equals("1") && sandc.equals("1")){
buttonSandc.setEnabled(true);
buttonSandc.setEnabled(true);
buttonSandc.setEnabled(true);
SharedPreferences.Editor editor = pref.edit();
editor.putString("MANDF", "0");
editor.putString("PANDR", "0");
editor.putString("SANDC", "0");
editor.apply();
}
}
// Button for Pick and Roll, where sharedpreferences assigns offense for use in another activity.
public void submitPANDR (View view) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Pick and Roll");
editor.putString("PANDR", "1");
editor.apply();
}
// Button for Move and Flow, where sharedpreferences assigns offense for use in another activity.
public void submitMANDF (View v) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Move and Flow");
editor.putString("MANDF", "1");
editor.apply();
}
// Button for Set and Called, where sharedpreferences assigns offense for use in another activity.
public void submitSANDC (View v) {
Intent intent = new Intent(this, ExchangeAToB.class);
startActivity(intent);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("offense", "Set and Called");
editor.putString("SANDC", "1");
editor.apply();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_team_aoffense, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}