質問

Sorry for this Silly question, actually i am new in Android... Actually I have 7 Text Views and 1 image button on my screen, i want when i click on image Button then all text values changed(as i want text), remember that all text values are different from each other and then i again click on image Button then text value again change (as i want text) and it's run up-to 12 times and change values of text views by click image Button on 12 times. if you know it can done by and Array then plz comment.

  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>

My 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) {

   }
}
役に立ちましたか?

解決 2

The easy way is to instantiate all your TextView's and then use the setText() method to change their text.

textView1.setText("One");

The other way is to get all the TextViews as child of your parent layout (like LinearLayout or the one you are using and using getChildAt method)

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");
        }
    }
}

The first one is easier and simpler. I prefer this. Hope I helped. :)

EDIT - Supoose on first click of the button you want the TextViews to have name of days and ont the next it displays numbers, the code will be:

 //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;**      
 }

他のヒント

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;
    }
}

And then:

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

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

And then use a list String to set text of them

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

In addition to the other posts: You could just use a global integer to keep track of your buttons state. In your onClick you add 1 to that integer, and depending on the integer value you can set the text to your textfields in a switch case. You would have 12 cases, so it won't look too nice, but that might be the easiest solution.

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

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

...and so on.

     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;
                }



    }   
});

You also have to put the text in your arrrays somewhere.

try out this

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");
   }
  }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top