在让2个按钮工作时遇到一些麻烦。
在下面的屏幕截图中,我制作了两个名为NEXT&的按钮。背部。这些按钮允许用户将其从第1周更改为第3周。当他们单击“下一步”并到达“周”时,我需要禁用该按钮,并且不允许它们继续进行,因为它最多只有3周。同样的事情是BACK按钮和骑自行车到达第1周。参见下面的代码。
http://i.stack.imgur.com/fLazK.jpg
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
final String PREFS_NAME = "MyPrefs";
ViewPager viewPager;
String week1 = "1";
String week2 = "2";
String week3 = "3";
private int[] weekNumbers = {1, 2, 3};
int weekNumberCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Casting variables
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
//////// First startup screen after first time use
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("my_first_time", true)) {
Intent VirginBoot = new Intent(this, Begin_Program.class);
startActivity(VirginBoot);
// Record the fact that the app has been started at least once
settings.edit().putBoolean("my_first_time", false).apply();
}
}
public void settingsButton(View view) {
Intent i = new Intent(this, Settings.class);
startActivity(i);
}
public void NextWeek(View view) {
int weekCounter = 0;
TextView weekNumber = (TextView) findViewById(R.id.week_number_text);
ImageButton LastWeekButton = (ImageButton) findViewById(R.id.LastWeek);
ImageButton NextWeekButton = (ImageButton) findViewById(R.id.NextWeek);
switch (view.getId()) {
case R.id.NextWeek:
if (weekCounter < (weekNumbers.length) - 2) {
weekCounter++;
weekNumber.setText(week2);
if (weekCounter == (weekNumbers.length) - 1) {
weekNumber.setText(week3);
LastWeekButton.setClickable(true);
NextWeekButton.setClickable(false);
}
}
case R.id.LastWeek:
if (weekCounter > weekNumbers.length) {
weekCounter--;
weekNumber.setText(week2);
if (weekCounter == (weekNumbers.length) - 1) {
weekNumber.setText(week1);
NextWeekButton.setClickable(true);
LastWeekButton.setClickable(false);
}
}
break;
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
Fragment fragment = null;
if (arg0 == 0) {
fragment = new Day_1();
}
if (arg0 == 1) {
fragment = new Day_2();
}
if (arg0 == 2) {
fragment = new Day_3();
}
if (arg0 == 3) {
fragment = new Day_4();
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
}
也许我正在寻找错误的区域,但我已尝试过以下链接,但到目前为止他们还没有工作过。
How to disable button click? How to disable button as soon as its clicked Click Limiting on <button>
答案 0 :(得分:0)
首先,您应该将按钮存储在班级中的字段中,而不是每次调用函数时都找到它们,而是在活动onCreate()中第一次找到它们。然后,在按钮的OnClick中,您可以在该按钮上调用setEnabled(false),具体取决于是否仍应启用该按钮。