如何在Android中单击一次imageButton一次更改7个textview值?

时间:2014-02-22 10:45:22

标签: android eclipse android-layout textview settext

对不起这个愚蠢的问题,实际上我是Android新手...... 实际上我的屏幕上有7个文本视图和1个图像按钮,我希望当我点击图像按钮然后所有文本值都改变了(因为我想要文本),记住所有文本值彼此不同然后我再次点击图像按钮然后文本值再次更改(因为我想要文本)并且它最多运行12次并通过单击图像按钮更改文本视图的值12次。 如果你知道它可以通过和Array完成那么请注释。

  My 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" >

  <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:src="@drawable/pictitle
    android:onClick="nextText"  />

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
 <TextView 
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
 <TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
</RelativeLayout>

我的Java:

     public class MyProject extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_pics);
}

public void indexPics(View v) {
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

public void nextText(View view) {

   }
}

4 个答案:

答案 0 :(得分:1)

public static int getIdByName(final String name) {
    try {
        final Field field = R.id.class.getDeclaredField(name);

        field.setAccessible(true);
        return field.getInt(null);
    } catch (Exception ignore) {
        return -1;
    }
}

然后:

final TextView[] txts= new TextView[7];

for (int i = 0; i < txts.length; i++) {
    txts[i] = (TextView) findViewById(getIdByName("textView" + (i + 1)));
}

然后使用列表字符串来设置它们的文本

    for (int i = 0; i < 7; i++) {
    txts[i].setText(StringList[i]);
}

答案 1 :(得分:0)

简单的方法是实例化所有TextView,然后使用setText()方法更改其文本。

textView1.setText("One");

另一种方法是将所有TextViews作为父布局的子项(如LinearLayout或您正在使用的那个并使用getChildAt方法)

LinearLayout ll = //Your Layout this can be any Linear or Relative layout 
                 //in which you added your spinners at runtime ;

int count = ll.getChildCount();
for(int i =0;i<count;i++)
{
    View v = ll.getChildAt(i);
    if(v instanceof TextView)
    {
        // you got the spinner
        EditText s = (EditText) v;
        if(s.getID() == R.id.textView1){
            textView1.setText("One");
        }
    }
}

第一个更简单,更简单。我更喜欢这个。希望我帮忙。 :)

编辑 - 首先单击按钮,希望TextViews具有天数名称,然后在下一步显示数字,代码将为:

 //You can also use List here
 String [] days = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
 String [] nums = {"1","2","3","4","5","6","7"};

 //Your method to update the TextViews
 public void updateTextViews(String [] texts){
         textView1.setText(texts[0]);
         textView2.setText(texts[1]);
         textView3.setText(texts[2]);
         textView4.setText(texts[3]);
         textView5.setText(texts[4]);
         textView6.setText(texts[5]);
         textView7.setText(texts[6]);
 }

 //Count the number of clicks, (or the condition on which you want to change the text displayed)
 int counter = 1;

 //your ImageBUtton's onClick() method
 @override
 public void onClick(View v){

    //Check the click count (or your condition)
    switch(counter){
        case 1: //first click
           updateTextViews(days);
           break;
        case 2: //second click
           updateTextViews(nums);
           break;
        default:
           //This is executed when no case matches. 
           break;
    } 
    **counter = counter + 1;**      
 }

答案 2 :(得分:0)

试试这个

public class MyProject extends Activity {
 TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.my_pics);
 tv1 = (TextView)findViewById(R.id.textView1);
 tv2 = (TextView)findViewById(R.id.textView2);
 tv3 = (TextView)findViewById(R.id.textView3);
 tv4 = (TextView)findViewById(R.id.textView4);
 tv5 = (TextView)findViewById(R.id.textView5);
 tv6 = (TextView)findViewById(R.id.textView6);
 tv7 = (TextView)findViewById(R.id.textView7);
}

public void indexPics(View v) {
 Intent i = new Intent(this, MainActivity.class);
 startActivity(i);
}

public void nextText(View view) {
  tv1.setText("1");
  tv2.setText("2");
  tv3.setText("3");
  tv4.setText("4");
  tv5.setText("5");
  tv6.setText("6");
  tv7.setText("7");
   }
  }

答案 3 :(得分:0)

除了其他帖子:您可以使用全局整数来跟踪按钮状态。在onClick中,您将1添加到该整数,并且根据整数值,您可以在切换案例中将文本设置为文本字段。你会有12个案例,所以看起来不太好,但这可能是最简单的解决方案。

switch(yourInteger)
{
    case 1:    textView1.setText(array1[0])
               textView2.setText(array2[0])
               ...

    case 2:    textView1.setText(array1[1])
               ...
}

......等等。

     public class MyProject extends Activity {

Button yourButton;
int buttonState;
String[] array1;
String[] array2;
//other arrays


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_pics);

    buttonState = 0;
    yourButton = (Button)findViewById(R.id.yourButton);
}

public void indexPics(View v) {
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

public void nextText(View view) {

   }
}

yourButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) { 

                buttonState++;

                TextView textView1 = (TextView)findViewById(R.id.textView1);
                TextView textView2 = (TextView)findViewById(R.id.textView2);
                //do the same for the other textviews here

                switch(buttonState)
                {
                    case 1: 
                           try{
                               Log.d("OnClick", "tv1 value on first click: "+array1[0]);
                           }catch(Exception e){}
                           //textView1.setText(array1[0]);
                           //textView2.setText(array2[0]);
                           textView1.setText("tv1: 1st click");
                           textView2.setText("tv2: 1st click");
                           //other textview here
                           break;
                    case 2:
                           try{
                               Log.d("OnClick", "tv1 value on second click: "+array1[1]);
                           }catch(Exception e){}
                           //textView1.setText(array1[1]); 
                           textView1.setText("tv1: 2nd click");
                           //other textviews here
                           break;
                    case 3: //and so on for all 12 states. Also dont forget:
                    ...
                    case 12:
                            buttonState = 0;
                }



    }   
});

你还必须将文本放在你的arrrays中。