添加带动态标签的动态按钮?

时间:2014-07-23 09:49:29

标签: android button dynamic numbers labels

我正在创建一个Android应用,我希望在其中添加button动态labelActivity的流程如下: -

  • 当主Activity加载时,它会询问用户" HOW MANY BUTTONS USER WANTS?" 例如: - 如果用户输入4
  • 新活动将以4 EditText(s)打开,其中用户将提供Buttons的名称 例如: - 播放,暂停,停止,倒带

  • 当用户提供姓名时,最终Activity会打开四个按钮,其名称为PLAY,PAUSE,STOP,REWIND。

我完成了第1部分和第2部分但面临第3部分的问题。有任何帮助吗?

第一项活动代码: -

package com.example.dynamicbutton;
public class MainActivity extends Activity
{
    private Button button,button1;
    EditText et;
    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1 = (Button) findViewById(R.id.addnames);
            et = (EditText) findViewById(R.id.editText);
            button1.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View arg0)
                {
                    String theText = et.getText().toString();
                    Intent intent = new Intent(MainActivity.this,NameActivity.class);
                    intent.putExtra("text_names", theText);
                    startActivity(intent);
                }
            });
        }
    }

第二项活动的代码: -

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        String m = extras.getString("text_label");
        int m1=Integer.parseInt(m);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamically_create_view_element);

        final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);

        // create the layout params that will be used to define how your
        // button will be displayed
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        //Create four 
        for(int j=1;j<=m1;j++) 
        {   
            // Create LinearLayout
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);

            // Create Button
            final Button btn = new Button(this);
            // Give button an ID
            btn.setId(j+1);
            btn.setText("Add To Cart"+j );
            // set the layoutParams on the button
            btn.setLayoutParams(params);

            final int index = j;

            btn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    Log.i("TAG", "index :" + index);

                    Toast.makeText(getApplicationContext(), "Clicked Button Index :" + index, Toast.LENGTH_LONG).show();

                }
            });

            //Add button to LinearLayout
            ll.addView(btn);
            //Add button to LinearLayout defined in XML
            lm.addView(ll);  
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这很容易。 只需按照以下步骤操作: -

  1. 创建一个只有Button作为子对象的Layout
  2. 创建另一个仅使用EditText作为子对象的布局。
  3. 创建第三个布局,其中没有对象。这将是您的最终布局,您将在其中显示播放,暂停,停止,倒带等按钮。您可以使用LinearLayout
  4. 创建 MainActivity ,用户将在其中输入所需的按钮数。
  5. 现在,在您的Java代码中,在MainActivity中,当用户输入他想要的按钮数时,将他迁移到下一个在Intent中传递数字的活动。
  6. OnCreate SecondActivity ,使用LayoutInflater类在For-Loop中对EditText进行充气,以动态加载该布局用户输入的次数。
  7. 将按钮的标题保存在ArrayList中并将其传递给 ThirdActivity
  8. OnCreate ThirdActivity ,使用LayoutInflater动态加载按钮布局,并从ArrayList
  9. 设置各自的文字

答案 1 :(得分:-1)

以下是您最终活动的代码

import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FinalActivity extends Activity {


String caption[4]={"play","edit","undo","delete"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YourLayout);

        final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain); // linearMain //is your linearlayout in XML file

        // create the layout params that will be used to define how your
        // button will be displayed
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        //Create four 
        for(int j=0;j<=4;j++) 
        {   
            // Create LinearLayout
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);

            // Create TextView
            Button btn= new Button(this);
            btn.setText(caption[j]+j+"    ");
            ll.addView(btn);

            // Create TextView
            TextView lbl= new TextView(this);
            price.setText(j+"     ");
            ll.addView(lbl);

            // Create Button
            final Button btn = new Button(this);
                // Give button an ID
                btn.setId(j+1);
                btn.setText("Add To Cart");
                // set the layoutParams on the button
                btn.setLayoutParams(params);

                final int index = j;
                // Set click listener for button
                btn.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        Log.i("TAG", "index :" + index);

                        Toast.makeText(getApplicationContext(), 
                                "Clicked Button Index :" + index, 
                                Toast.LENGTH_LONG).show();

                    }
                });

               //Add button to LinearLayout
                ll.addView(btn);
               //Add button to LinearLayout defined in XML
                lm.addView(ll);  
        }
    }
}