【问题标题】:Button to show previous string显示上一个字符串的按钮
【发布时间】:2012-08-26 21:25:12
【问题描述】:

我正在开发一个带有下一个/上一个和复制按钮的报价应用程序。

这是代码:

    Button btn1;
     String countires[];
     int i=0;
        /** Called when the activity is first created. */
     @Override
      public void onCreate(Bundle savedInstanceState)
     {
     super.onCreate(savedInstanceState);
         setContentView(R.layout.prob2);

btn1 = (Button) findViewById(R.id.prob2_btn1);

countires = getResources().getStringArray(R.array.country);

for (String string : countires)
{
    Log.i("--: VALUE :--","string = "+string);
}

btn1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub
        String  country  = countires[i];
        btn1.setText(country);
        i++;
        if(i==countires.length)
            i=0;
    }
});
}

我需要“上一个”按钮的 onClick 代码来在 textView 中显示上一个字符串???

【问题讨论】:

    标签: android string button


    【解决方案1】:

    为您的活动创建一个新成员,例如:

    int actual = 0;
    

    然后创建一个“下一步”按钮:

    nextButton = (Button) findViewById(...);
    
    nextButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            actual = actual < countires.length - 1 ? actual + 1 : actual;
            String  country  = countires[actual];
            btn1.setText(country);
        }
    });
    

    上一个按钮也是如此:

    prevButton = (Button) findViewById(...);
    
    prevButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            actual = actual > 0 ? actual - 1 : actual;
            String  country  = countires[actual];
            btn1.setText(country);
        }
    });
    

    【讨论】:

    • 我不是专家,但我很高兴能帮上忙 :)
    【解决方案2】:

    那就是:

    // Prev
    if ( i > 0 ) {
        i--;
    } else {
        i = countires.length - 1;
    }
    String  country  = countires[i];
    btn1.setText(country);
    

    编辑:最有意义的是也更改下一个按钮。因为在下一个方法中,您现在在设置文本后增加 i 。逻辑有点混乱。

    // Next
    if ( i < countires.length - 1 ) {
        i++;
    } else {
        i = 0;
    }
    String  country  = countires[i];
    btn1.setText(country);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 2020-10-30
      • 1970-01-01
      • 2015-05-09
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多