0

同様の種類のアプリを開発したい (下のリンク) Android で動的にボタンを作成するには? しかし同時に、これを同じアクティビティではなく別のアクティビティで表示したいと考えています。2 つの編集テキストがあります。1) 作成するボタン名。2) 宛先アドレス (新しいボタンの作成時に送信されるメッセージ用)。そのテキストは、新しいボタンを作成するために別のアクティビティに渡されます。

私が書いたとき

 public void onClick(View v) {    
            // TODO Auto-generated method stub    
        final Context context1=this;    
               if(v.getId()==R.id.button4){    
     LinearLayout l1 = (LinearLayout) findViewById(R.id.layout);      
  // R.id.layout is the layout id of the xml file for the 2nd activity.    
    Intent intent1 = new Intent(context1,PCode.class);    
    Button b = new Button(this);    
    l1.addView(b);    
    startActivity(intent1);    

 }        

アクティビティは 2 番目のアクティビティに移行せず、プログラムは終了しています。同じアクティビティで行うと、新しいボタンを作成できます。親切に助けてください。

4

2 に答える 2

0

ボタンごとにインテントを介して合計 3 つのメッセージを渡すことができます。 1) 作成されるボタン名。2) 宛先アドレス (新しいボタンの作成時に送信されるメッセージ用)。3) ボタン アクション (追加/削除) 新しいアクティビティでは、3 番目のインテント メッセージ、つまりボタン アクション (追加/削除) を使用してボタン アクションを処理します。新しいアクティビティでは、以下のコードを使用して処理できます

  boolean isAddButton = getIntent().getBooleanExtra("ButtonAction", false);
            if(isAddButton){
             Button myButton = new Button(this);
             myButton.setText("Add Me");
             LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
             LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.WRAP_CONTENT);
             ll.addView(myButton, lp);
            }else{
             Button myButton = new Button(this);
             myButton.setText("Remove Me");

             LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
             LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.WRAP_CONTENT);
             ll.removeView(myButton, lp);
       }
于 2013-06-12T09:26:58.803 に答える
0

最初のアクティビティの onClick で、Intent を使用してデータを送信します。

intent = new Intent(this, PCode.class);
        intent.putExtra("EXTRA_BTN_NAME", editText.getText());
        intent.putExtra("EXTRA_WHERE", where);
        startActivity(intent);

新しいアクティビティでは、データを取得する必要があります

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activty2);

    Intent intent = getIntent();
    String btnName = intent.getStringExtra("EXTRA_BTN_NAME");
    where= intent.getStringExtra("EXTRA_WHERE");

LinearLayout l1 = (LinearLayout) findViewById(R.id.layout);     
Intent intent1 = new Intent(context1,PCode.class);    
Button b = new Button(this);    
b.SetText(btnName);
//TODO - use the "where" parameter
l1.addView(b);
于 2013-06-12T09:21:10.140 に答える