【问题标题】:How to generate multiple buttons that have the same intent but have another Extra如何生成具有相同意图但具有另一个 Extra 的多个按钮
【发布时间】:2015-11-24 18:34:23
【问题描述】:

我正在尝试使用 LayoutInflater 在 Eclipse 中生成多个视图。该按钮也有一个 OnClickListener 并且它可以工作。意图开始,我给它一个额外的。

但我想要的是一个按钮保留它自己的附加功能。说第一个按钮有额外的:数字 1 第二个有 2 等等。目前所有按钮都获得相同的额外值,编号 5。因为当我尝试在 com.example.http WIZARD 中获得额外的意图时,它总是说 5

 String[] title = {
        "1",
        "2",
        "3",
        "4",
        "5"
    };

LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll_content);
List<View> views = new ArrayList<View>();

for(int i=0; i<title.length; i++){
    View view = layoutInflator.inflate(R.layout.single_task, null);
    TextView textView = (TextView) view.findViewById(R.id.tvName);
    Button bStartWizard = (Button) view.findViewById(R.id.bStartWizard);

    textView.setText("Opdrachtnummer:" + title[i]);
    titleString = "Opdrachtnummer:" + title[i];

    bStartWizard.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent("com.example.http.WIZARD");
            i.putExtra("Number", titleString);
            startActivity(i);
        }
    });

    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    views.add(view);
}

for(int i = 0; i<views.size(); i++){
    insertPoint.addView((View) views.get(i));
}

希望有人能帮帮我!

【问题讨论】:

    标签: android eclipse android-intent layout-inflater


    【解决方案1】:

    Button bStartWizard = (Button) view.findViewById(R.id.bStartWizard);

    我认为您在这里遇到了问题,因为您想在 xml 上生成 5 个具有相同按钮 ID 的按钮,因此 onclicklistener 将尝试获取分配给按钮的最后一个值,即5、尝试为每个按钮生成不同的ID。

    在创建按钮时检查此代码,操作值

    Button bStartWizard = (Button) inflater.inflate(R.layout.play_game_option_button, layout, false);
    

    【讨论】:

    • 这个参数到底是什么意思:布局?
    猜你喜欢
    • 2022-10-25
    • 1970-01-01
    • 2017-11-26
    • 2021-03-02
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多