使用java单击按钮时交换两个文本视图值

时间:2017-05-30 05:13:16

标签: java android textview

enter image description here

我在Java中使用布局参数创建了一个动态UI,我想在单击图像视图时更改两个文本视图值。

public class Sorting_Activity extends AppCompatActivity {

  LinearLayout Sorting_Linear;
  private final Context mContext = Sorting_Activity.this;
  String[] outletname = {"kevan", "parth", "keyur", "k", "a"};
  Button submit;

  LinearLayout.LayoutParams nametxt = new LinearLayout.LayoutParams(0, 160, 3);
  LinearLayout.LayoutParams imagebtnup = new LinearLayout.LayoutParams(160, 0, 2);
  LinearLayout.LayoutParams imagebtndown = new LinearLayout.LayoutParams(160, 0, 2);
  LinearLayout.LayoutParams linearlayout = new LinearLayout.LayoutParams(0, 160, 1);


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

    //==========================================================
    //  Dynamic Designing Code
    //==========================================================

    nametxt.setMargins(4, 6, 0, 0);
    imagebtnup.setMargins(4, 6, 0, 0);
    imagebtndown.setMargins(4, 6, 0, 0);
    linearlayout.setMargins(4, 6, 0, 0);

    Sorting_Linear = (LinearLayout) findViewById(R.id.Sorting_Linear);
    submit = (Button) findViewById(R.id.submit);
    LinearLayout[] llRow = new LinearLayout[5];

    final TextView[] outletnametxt = new TextView[5];
    final ImageView[] imageButtonup = new ImageView[5];
    final ImageView[] imageButtondown = new ImageView[5];
    final LinearLayout[] Linearlayout = new LinearLayout[5];


    for (int i = 0; i < 5; i++) {

        llRow[i] = new LinearLayout(mContext);
        LinearLayout.LayoutParams paramsllRow = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsllRow.setMargins(0, 2, 0, 0);
        llRow[i].setLayoutParams(paramsllRow);
        llRow[i].setOrientation(LinearLayout.HORIZONTAL);

        outletnametxt[i] = new TextView(mContext);
        outletnametxt[i].setLayoutParams(nametxt);
        outletnametxt[i].setText(outletname[i]);
        outletnametxt[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        outletnametxt[i].setTextSize(20);
        outletnametxt[i].setGravity(Gravity.CENTER);
        outletnametxt[i].setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        llRow[i].addView(outletnametxt[i]);


        imageButtonup[i] = new ImageView(mContext);
        imageButtonup[i].setLayoutParams(imagebtnup);
        imageButtonup[i].setImageResource(R.drawable.arrowup);


        imageButtondown[i] = new ImageView(mContext);
        imageButtondown[i].setLayoutParams(imagebtndown);
        imageButtondown[i].setImageResource(R.drawable.arrowdown);


        Linearlayout[i] = new LinearLayout(mContext);
        Linearlayout[i].setLayoutParams(linearlayout);
        Linearlayout[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        Linearlayout[i].setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        Linearlayout[i].setOrientation(LinearLayout.VERTICAL);
        Linearlayout[i].addView(imageButtonup[i]);
        Linearlayout[i].addView(imageButtondown[i]);
        llRow[i].addView(Linearlayout[i]);

        Sorting_Linear.addView(llRow[i]);


     }
  }
}

以下是完整的设计代码,我想在点击imageButtondownimagebuttonup时更改textview的值。

1 个答案:

答案 0 :(得分:0)

将以下代码放在for循环

的末尾
final int itr = i;
imageButtonup[i].setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        int index = itr < outletnametxt.length ? itr+1 : itr;
        String val = outletnametxt[index].getText().toString();//index of text value to get value from
        outletnametxt[index].setText(outletnametxt[itr].getText().toString());
        outletnametxt[itr].setText(val);
    }
});


imageButtondown[i].setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        int index = itr > 0 ? itr-1 : itr;
        String val = outletnametxt[index].getText().toString();
        outletnametxt[index].setText(outletnametxt[itr].getText().toString());
        outletnametxt[itr].setText(val);
    }
});