Android 动态添加textView或EditText,并获取输入数据

  • 问题引入
    Android开发中往往需要动态创建诺干个同类控件,或对他们设置点击事件或批量获取他们的值,下面提供我在开发中所使用的一种参考。

  • 动态创建多个textView,这里描述一个页面取名为:test.xml,现在在Id为MyTable的控件里插入多个textview控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none"
        >

        <HorizontalScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbarAlwaysDrawHorizontalTrack="false"
            android:scrollbars="none">

            <LinearLayout
                android:id="@+id/MyTable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="9dp"
                android:orientation="vertical"
                >
            </LinearLayout>

        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>
  • java代码创建如下:
LinearLayout mainLinerLayout = (LinearLayout) this.findViewById(R.id.MyTable);
TextView textview=new TextView(this);
textview.setText("你好!");
mainLinerLayout.addView(textview);

同理EditText创建方式一样,不在赘述。(将TextView替换为EditView即可)

  • 对于动态创建的控件是不能通过id进行关联的,因而想要获取他的内容相对不易,这里介绍一下对于动态创建的EditText,如何获取他的值。
    在创建时对editText设置监听(TextWatcher),重写里面的 三个TextChanged函数,完成你所需要的操作
    ps:因为在动态创建,不同于在xml直接写的,所以在设置监听时,会让你将控件设为final,在重写的函数里,涉及到的也必须是final型变量,获取里面的内容可以存在一个外部final变量中,然后操控外部变量进行读取数据。
LinearLayout mainLinerLayout = (LinearLayout) this.findViewById(R.id.MyTable);
final int cout=i;//用于监听定位控件
final EditText editText=relativeLayout.findViewById(R.id.edit_num);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//输入内容之前你想做什么
                }

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//输入的时候你想做什么
                }

@Override
public void afterTextChanged(Editable s) {
    //输入之后你想做什么                num[cout]=Integer.parseInt(editText.getText().toString());
                }
            });
mainLinerLayout.addView(editText);

同理对TextView可以设置监听事件,获取或设置你的需求。

以上便是我对这方面的理解,欢迎大家提出问题一起讨论。

一次性获取有命名规则的所有Android控件

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
好的,我可以为您提供一些代码来实现这个功能。 首先,您需要在 Android Studio 中创建一个新的 Android 项目,然后在布局文件中添加一个 EditText、一个 Button 和一个 TextView。 接下来,您可以在按钮的 onClickListener 中编写代码,以便在 EditText输入内容时将其写入文件,然后在 TextView 中读取该文件的内容。这里是一些示例代码: ```java public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.edit_text); textView = findViewById(R.id.text_view); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取 EditText 中的文本 String text = editText.getText().toString(); // 将文本写入文件 try { FileOutputStream fos = openFileOutput("data.txt", MODE_PRIVATE); fos.write(text.getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } // 从文件中读取文本并显示在 TextView 中 try { FileInputStream fis = openFileInput("data.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); String fileContent = new String(buffer); textView.setText(fileContent); } catch (IOException e) { e.printStackTrace(); } } }); } } ``` 在这个示例代码中,我们使用了 openFileOutput 和 openFileInput 方法来创建和读取文件。这些方法将文件保存在应用程序的私有存储空间中,可以通过 DDMS 工具来查看。 您可以按照这个示例代码来编写您的 Android 应用程序,并在 DDMS 中查看文件是否成功写入和读取。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林深人不知

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值