如何通过使用数组和开关盒点击单个按钮来处理文本视图和图像,我需要Android中previos按钮的代码



极客!我是安卓系统的初学者。。。我正在我的应用程序中进行android的另一个活动,在这个活动中,我总共有10个视图(7个文本视图+2个图像按钮+1个图像视图(和两个图像按钮(即下一个和上一个(,我通过单击单个下一个按钮一次更改所有这10个视图,我使用了array和switch来更改文本值和图像。原因是所有的tex值彼此不同,但问题是我在它的UI.xml文件中使用了这些值中的第一个,而没有包含在array中,这就是为什么当我单击后退按钮时,它无法显示所有这些值的第一个值。其次,默认情况下,当用户第一次查看时,我还想看不见上一个按钮,当用户点击下一个按钮一次时,两个按钮都会显示出来,当用户第三次点击时,这两个按钮又会显示出来?我被困在这里,而且我不知道上一个按钮的正确代码(比如increment(,这就是为什么我没有在这里添加。若你们理解我想要做什么,那个么编辑我的代码或者发布具有相同功能的新代码。我们将感谢你的帮助。

  My Java Code:
  public class AlifPics extends Activity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
ImageView img1;
ImageButton imgBut1;
ImageButton imgBut2;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alif_pics);
    text1 = (TextView) findViewById(R.id.textView1);
    text2 = (TextView) findViewById(R.id.textView2);
    text3 = (TextView) findViewById(R.id.textView3);
    text4 = (TextView) findViewById(R.id.textView4);
    text5 = (TextView) findViewById(R.id.textView5);
    text6 = (TextView) findViewById(R.id.textView6);
    text7 = (TextView) findViewById(R.id.textView7);
    img1 = (ImageView) findViewById(R.id.imageView1);
    imgBut1 = (ImageButton) findViewById(R.id.imageButton1);
    imgBut2 = (ImageButton) findViewById(R.id.imageButton2);
}
// this Menu Button method goes to MainActivity
public void indexHuroof(View v) {
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}
 int counter = 1;
String[] textvalue1 = { "a2", "a3" };
String[] textvalue2 = { "b2", "b3" };
String[] textvalue3 = { "c2", "c3" };
String[] textvalue4 = { "d2", "d3" };
String[] textvalue5 = { "e2", "e3" };
String[] textvalue6 = { "f2", "f3" };
String[] textvalue7 = { "g2", "g3" };
int[] myImageViewList1 = new int[] { R.drawable.img2, R.drawable.img3 }; // 123
int[] myImageButtonList1 = new int[] { R.drawable.ibut2, R.drawable.ibut3 }; // 1,2,3
int[] myImageButtonList2 = new int[] { R.drawable.ibut5, R.drawable.ibut6 }; // 4,5,6
// my next ImageButton
public void nextClick(View v) {
    try {
        switch (counter) {
        case 1: // first click
            text1.setText(textvalue1[0]);
            text2.setText(textvalue2[0]);
            text3.setText(textvalue3[0]);
            text4.setText(textvalue4[0]);
            text5.setText(textvalue5[0]);
            text6.setText(textvalue6[0]);
            text7.setText(textvalue7[0]);
            img1.setImageResource(myImageViewList1[0]);
            imgBut1.setImageResource(myImageButtonList1[0]);
            imgBut2.setImageResource(myImageButtonList2[0]);
            break;
        case 2: // second click
            text1.setText(textvalue1[1]);
            text2.setText(textvalue2[1]);
            text3.setText(textvalue3[1]);
            text4.setText(textvalue4[1]);
            text5.setText(textvalue5[1]);
            text6.setText(textvalue6[1]);
            text7.setText(textvalue7[1]);
            img1.setImageResource(myImageViewList1[1]);
            imgBut1.setImageResource(myImageButtonList1[1]);
            imgBut2.setImageResource(myImageButtonList2[1]);
            break;
        case 3: // third click
            text1.setText(textvalue1[2]);
            text2.setText(textvalue2[2]);
            text3.setText(textvalue3[2]);
            text4.setText(textvalue4[2]);
            text5.setText(textvalue5[2]);
            text6.setText(textvalue6[2]);
            text7.setText(textvalue7[2]);
            img1.setImageResource(myImageViewList1[2]);
            imgBut1.setImageResource(myImageButtonList1[2]);
            imgBut2.setImageResource(myImageButtonList2[2]);
            break;
        default:
            break;
        }
    } catch (Exception e) {
    }
    counter++;
}
// my previous ImageButton
public void previousClick() {
    try {
    } catch (Exception e) {
    }
}

}

根据您的代码,我的建议是:

  1. 定义您的计数器global和Static(在OnCreate之前(,使Value可靠
  2. 你可以将所有的文本保存到String.xml中,你的编码会更漂亮,你可以这样使用它们:

    getResources((.getString(R.string.yourString(;

  3. 创建一个函数来处理create-like中所有必须加载的字符串这个:

    protected void onCreate(Bundle savedInstanceState({super.onCreate(savedInstanceState(;setContentView(R.layout.alif_pics(;

    preLoadText();
    initilizeUi();
    }
    void preLoadText(){
    // put all text stuffs
    }
    void initilizeUi(){
    // put you main application code
    }
    

您可以通过将其添加到您的oncreate方法中来隐藏背面图像按钮:

imgBut1.setEnabled(false);
imgBut1.setClickable(false);

其次,您可以将默认值存储在项目的/res/values/文件夹中的strings.xml文件中,以便以后可以保留这些值。

<string name="textview1_deafult">TextView1 Deafult value</string>

当计数器达到零时,您可以添加以下代码来禁用后退按钮:

// my previous ImageButton
public void previousClick() {
    try {
        imgBut1.setClickable(false);
        imgBut1.setEnabled(false);
        text1.setText(R.string.textview1_default);
        //and so on
    } catch (Exception e) {
    }
}

不要忘记使用带小写"s"的"字符串"R.string.textview1_default">

My Java Code:
public class AlifPics extends Activity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
ImageView img1;
ImageButton imgBut1;
ImageButton imgBut2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alif_pics);
text1 = (TextView) findViewById(R.id.textView1);
text2 = (TextView) findViewById(R.id.textView2);
text3 = (TextView) findViewById(R.id.textView3);
text4 = (TextView) findViewById(R.id.textView4);
text5 = (TextView) findViewById(R.id.textView5);
text6 = (TextView) findViewById(R.id.textView6);
text7 = (TextView) findViewById(R.id.textView7);
img1 = (ImageView) findViewById(R.id.imageView1);
imgBut1 = (ImageButton) findViewById(R.id.imageButton1);
imgBut2 = (ImageButton) findViewById(R.id.imageButton2);
//considering imgBut1 is back button and imgBut2 is next button
imgBut1.setEnabled(false);//disable back button at start
imgBut1.setClicakble(false);
}   
// this Menu Button method goes to MainActivity
public void indexHuroof(View v) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
int counter = 1;
String[] textvalue1 = { "a2", "a3" };
String[] textvalue2 = { "b2", "b3" };
String[] textvalue3 = { "c2", "c3" };
String[] textvalue4 = { "d2", "d3" };
String[] textvalue5 = { "e2", "e3" };
String[] textvalue6 = { "f2", "f3" };
String[] textvalue7 = { "g2", "g3" };
int[] myImageViewList1 = new int[] { R.drawable.img2, R.drawable.img3 }; // 123
int[] myImageButtonList1 = new int[] { R.drawable.ibut2, R.drawable.ibut3 }; // 1,2,3
int[] myImageButtonList2 = new int[] { R.drawable.ibut5, R.drawable.ibut6 }; // 4,5,6
// my next ImageButton
public void nextClick(View v) {
try {
    switch (counter) {
    case 1: // first click
        text1.setText(textvalue1[0]);
        text2.setText(textvalue2[0]);
        text3.setText(textvalue3[0]);
        text4.setText(textvalue4[0]);
        text5.setText(textvalue5[0]);
        text6.setText(textvalue6[0]);
        text7.setText(textvalue7[0]);
        img1.setImageResource(myImageViewList1[0]);
        imgBut1.setImageResource(myImageButtonList1[0]);
        imgBut2.setImageResource(myImageButtonList2[0]);
        imgBut1.setEnabled(true);//enable back button
        imgBut1.setClickable(true);
        break;
    case 2: // second click
        text1.setText(textvalue1[1]);
        text2.setText(textvalue2[1]);
        text3.setText(textvalue3[1]);
        text4.setText(textvalue4[1]);
        text5.setText(textvalue5[1]);
        text6.setText(textvalue6[1]);
        text7.setText(textvalue7[1]);
        img1.setImageResource(myImageViewList1[1]);
        imgBut1.setImageResource(myImageButtonList1[1]);
        imgBut2.setImageResource(myImageButtonList2[1]);
        break;
    case 3: // third click
        text1.setText(textvalue1[2]);
        text2.setText(textvalue2[2]);
        text3.setText(textvalue3[2]);
        text4.setText(textvalue4[2]);
        text5.setText(textvalue5[2]);
        text6.setText(textvalue6[2]);
        text7.setText(textvalue7[2]);
        img1.setImageResource(myImageViewList1[2]);
        imgBut1.setImageResource(myImageButtonList1[2]);
        imgBut2.setImageResource(myImageButtonList2[2]);
        imgBut2.setEnabled(false);
        imgBut2.setClickable(false);
        break;
    default:
        break;
    }
} catch (Exception e) {
}
counter++;
}
// my previous ImageButton
public void previousClick() {
   switch (counter) {
    case 4: 
        text1.setText(textvalue1[1]);
        text2.setText(textvalue2[1]);
        text3.setText(textvalue3[1]);
        text4.setText(textvalue4[1]);
        text5.setText(textvalue5[1]);
        text6.setText(textvalue6[1]);
        text7.setText(textvalue7[1]);
        img1.setImageResource(myImageViewList1[1]);
        imgBut1.setImageResource(myImageButtonList1[1]);
        imgBut2.setImageResource(myImageButtonList2[1]);
        imgBut2.setEnabled(true);
        imgBut2.setClickable(true);
        break;
    case 3: 
        text1.setText(textvalue1[0]);
        text2.setText(textvalue2[0]);
        text3.setText(textvalue3[0]);
        text4.setText(textvalue4[0]);
        text5.setText(textvalue5[0]);
        text6.setText(textvalue6[0]);
        text7.setText(textvalue7[0]);
        img1.setImageResource(myImageViewList1[0]);
        imgBut1.setImageResource(myImageButtonList1[0]);
        imgBut2.setImageResource(myImageButtonList2[0]);
        break;
    case 2: 
        text1.setText(R.string.textview1_default);
        text2.setText(R.string.textview1_default);
        text3.setText(R.string.textview1_default);
        text4.setText(R.string.textview1_default);
        text5.setText(R.string.textview1_default);
        text6.setText(R.string.textview1_default);
        text7.setText(R.string.textview1_default);
        img1.setImageResource(R.drawable.your_default_drawable_for_img1);
        imgBut1.setImageResource(R.drawable.your_default_drawable_for_imgBut1);
        imgBut2.setImageResource(R.drawable.your_default_drawable_for_imgBut2);
        imgBut1.setEnabled(false);
        imgBut1.setClickable(false);
        break;
    default:
        break;
    }
} catch (Exception e) {
}
counter--;
}

这应该可以完成

最新更新