代码之家  ›  专栏  ›  技术社区  ›  olirwin

如何在活动中添加数量可变的TextView和EditView

  •  2
  • olirwin  · 技术社区  · 6 年前

    我目前正在尝试向活动中添加用户定义数量的TextView和EditText,但除了通过创建各种不同的活动对其进行硬编码之外,似乎无法做到这一点。

    这项活动的目的是记下球员的名字,球员的数量由前一项活动的额外意图传递。

    我正在尝试添加一个文本视图,上面写着“Player X:”,以及一个EditText,用于为每个玩家键入玩家的名称

    我从这篇文章中了解到: How to create a variable number of textviews in android 但是,我必须以编程方式完成这项工作,这似乎对我不起作用(测试时,活动仍然为空)

    我尝试创建了一个临时线性布局,在for()中添加了两个视图,但仍然没有结果。

    有什么想法吗?我走对了吗?

    顺致敬意,

    [编辑]代码在此处:

    public class players extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_players);
    
        Bundle extras = new Bundle();
        final int numPlayers = extras.getInt("num");
        final LinearLayout myLayout = findViewById(R.id.lin_Re);
        final ArrayList<Player> players = new ArrayList<>();
    
    
    
        int size = numPlayers; // total number of TextViews to add
    
        TextView[] tv = new TextView[size];
        TextView temp;
        EditText[] et = new EditText[size];
        EditText temp2;
        LinearLayout temp3;
    
        for (int i = 0; i < size; i++)
        {
            temp = new TextView(this);
            temp2 = new EditText(this);
            temp3 = new LinearLayout(this);
    
            temp.setText("Player " + i + " : "); //arbitrary task
    
            // add the textview to the linearlayout
            temp3.addView(temp);
            temp3.addView(temp2);
    
            tv[i] = temp;
            et[i] = temp2;
    
            myLayout.addView(temp3);
        }
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:id="@+id/lin_Re">
    
    
    
        </LinearLayout>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b_send"/>
    
    </LinearLayout>
    

    1 回复  |  直到 6 年前
        1
  •  3
  •   Tuấn Kiệt    6 年前

    有两种方法可以实现这一点:
    1、使用 RecyclerView [推荐]
    2、添加 TextView EditText (嵌套在水平 LinearLayout )变成垂直的 线性布局 嵌套在 ScrollView 以编程方式

    下面我描述的第一个解决方案非常简单,如果您熟悉 回收视图 ListView ,第二个解决方案(您当前的轨道)有点棘手,但仍然可以实现。

    解决方案1 :
    enter image description here
    enter image description here
    enter image description here

    主要活动

    public class MainActivity extends AppCompatActivity {
    
        RecyclerView mPlayerList;
        List<String> mPlayerNames;
        PlayerAdapter mAdapter;
        EditText mInput;
        Button mCreateButton;
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mPlayerNames = new ArrayList<>();
    
            // setup recycler view
            mPlayerList = findViewById(R.id.player_list);
            mPlayerList.setLayoutManager(new LinearLayoutManager(this));
            mAdapter = new PlayerAdapter();
            mPlayerList.setAdapter(mAdapter);
    
            // setup input EditText
            mInput = findViewById(R.id.input);
    
            // setup Create button
            mCreateButton = findViewById(R.id.create_button);
            mCreateButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // clear old player names
                    mPlayerNames.clear();
    
                    // read user input: number of player:
                    String input = mInput.getText().toString();
                    int numberOfPlayer;
                    try {
                        numberOfPlayer = Integer.parseInt(input);
                    } catch (NumberFormatException e) {
                        Toast.makeText(MainActivity.this, "Invalid input!!!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    for (int i = 0; i < numberOfPlayer; ++i) {
                        mPlayerNames.add("Player #" + (i + 1));
                    }
    
                    // make change on recycler view
                    mAdapter.notifyDataSetChanged();
    
                    // dismiss keyboard
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(mInput.getWindowToken(), 0);
    
                }
            });
        }
    
        private class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {
    
    
            @Override
            public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent, false);
                return new PlayerHolder(v);
            }
    
    
            @Override
            public void onBindViewHolder(PlayerHolder holder, int position) {
                holder.bind(mPlayerNames.get(position));
            }
    
            @Override
            public int getItemCount() {
                return mPlayerNames.size();
            }
    
            public class PlayerHolder extends RecyclerView.ViewHolder {
    
                TextView mPlayerLabel;
                EditText mPlayerName;
    
    
                public PlayerHolder(View itemView) {
                    super(itemView);
                    mPlayerLabel = itemView.findViewById(R.id.player_label);
                    mPlayerName = itemView.findViewById(R.id.player_name);
                }
    
                public void bind(String playerName) {
                    mPlayerLabel.setText(playerName);
                    mPlayerName.setHint("Name of " + playerName);
                }
            }
        }
    }
    

    示例项目可在此处找到:
    https://github.com/raiytu4/stackcase004