将2个或更多edittexts更改为textview android

时间:2016-08-22 08:42:40

标签: java android android-layout

您好我是Android的新手,我正在进行一项项目,我想要将多个EditText转换为Textviews。我需要一种方法来帮助我通过单击按钮来转换所有这些

主要活动

public class MainActivity extends AppCompatActivity {

    EditText[] editTextArray = new EditText[]{

            (EditText) findViewById(R.id.editText),
            (EditText) findViewById(R.id.editText3),
            (EditText) findViewById(R.id.editText4),
            (EditText) findViewById(R.id.editText5),
            (EditText) findViewById(R.id.editText6),
            (EditText) findViewById(R.id.editText7),
            (EditText) findViewById(R.id.editText8),
            (EditText) findViewById(R.id.editText9),
            (EditText) findViewById(R.id.editText10),
            (EditText) findViewById(R.id.editText11),
            (EditText) findViewById(R.id.editText12),
            (EditText) findViewById(R.id.editText13),
            (EditText) findViewById(R.id.editText14),
            (EditText) findViewById(R.id.editText15),
            (EditText) findViewById(R.id.editText16),
            (EditText) findViewById(R.id.editText17),
            (EditText) findViewById(R.id.editText18),
    };
    TextView[] textViews = new TextView[]
            {
                    (TextView) findViewById(R.id.textView15),
                    (TextView) findViewById(R.id.textView20),
                    (TextView) findViewById(R.id.textView21),
                    (TextView) findViewById(R.id.textView22),
                    (TextView) findViewById(R.id.textView23),
                    (TextView) findViewById(R.id.textView24),
                    (TextView) findViewById(R.id.textView26),
                    (TextView) findViewById(R.id.textView27),
                    (TextView) findViewById(R.id.textView28),
                    (TextView) findViewById(R.id.textView29),
                    (TextView) findViewById(R.id.textView30),
                    (TextView) findViewById(R.id.textView31),
                    (TextView) findViewById(R.id.textView32),
                    (TextView) findViewById(R.id.textView33),
                    (TextView) findViewById(R.id.textView34),
                    (TextView) findViewById(R.id.textView35),
                    (TextView) findViewById(R.id.textView36),

            };


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


        for (int x = 0; x < editTextArray.length; x++) {
            editTextArray[x].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String output = x.getText().toString();
                    textViews.setText(output);

                }
            });

        }
    }
}

1 个答案:

答案 0 :(得分:1)

而是从xml添加textview或edittext,您需要动态添加它们。

看一下这个链接

How can I add a TextView to a LinearLayout dynamically in Android?

通过这种方法,您可以很好地控制textview和edittext。

例如:点击按钮,隐藏编辑文本,并在该位置显示textview。