2
在獲取2個按鈕時遇到問題。Android:更改並限制按鈕點擊
在下面的屏幕截圖中,我製作了2個按鈕,名爲NEXT & BACK。這些按鈕允許用戶將其從第1周更改爲第3周。當他們點擊NEXT併到達Week時,我需要該按鈕禁用並且不允許他們繼續進行,因爲它只有最多3周。同樣的事情適用於後退按鈕和循環,並達到第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>
確定何時禁用以及何時啓用它的邏輯可能不正確。 –
也在這裏發佈你的xml佈局 – Manishika