2015-02-09 72 views
0

我在试图的是,用户将按下按钮并创建一个新按钮(按钮1)将出现,并从该按钮1他们将去一个不同的活动,你可以创建不止一个。并且在该活动完成之后,返回到启动此按钮的活动。到目前为止,我已成功通过这段代码做到这一点:如何动态地创建多个按钮并分别处理每个按钮

new_question = (Button)findViewById(R.id.new_question); 
    new_question.setOnClickListener(onClick()); 

private OnClickListener onClick() { 
    return new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mLayout.addView(createNewTextView()); 

      Button.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

       Intent myIntent = new Intent(getBaseContext(), Question.class); 
       startActivity(myIntent); 
       return; 

       } 

      }); 

     } 
    }; 
} 

TextView createNewTextView() { 
    final RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    Button = new Button(this); 
    lparams.addRule(RelativeLayout.ABOVE, R.id.linearLayout2); 
    Button.setLayoutParams(lparams); 
    Button.setText("New Question");  
    return Button; 
} 

我的第一个问题,我不知道如何以不同方式处理每一个按钮,因为我只创建一个。另一方面,我不想在xml文件中事先创建它们,它们必须通过java类编程。

第二,创建每个按钮后都能看到它们。因为在创建按钮之后,我正在经历下一个活动,然后回来(不是使用后退按钮,而是使用Intent)按钮不在那里。

任何提示?

回答

0

只是给每个按钮tag,并指定他们都在此进行测试什么这个按钮标记,然后根据所作出正确的动作相同onClickListener

mButton.setTag(i); 
i++; 
mButton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        int but_id = Integer.parseInt(v.getTag().toString().trim()); 
        if(but_id == 1){ 
         // Do Something 
        }else if(but_id == 2){ 
         // Do Another Something 
        } 
       } 

      }); 

更新:

这个标签就像你给视图的标签,以便在将来参考它,所以,当给按钮一个不同的标签时,你就像给他们一个ID,看看here获取更多信息。

关于按钮,不幸的是他们不会回来的时候在那里,你必须做下列之一:

  1. 重新创建。
  2. 从活动中移动它们时将它们传递并再次传递给活动意图,然后再次使用传递的数据重新创建它们。
  3. 只需将它们定义为static变量,并在返回时重新将它们添加到活动布局中的相应父级。
+0

你能解释我标签的工作原理吗? 他们是否会在我回到这个活动后在那里? – pap 2015-02-09 13:28:51

+0

@pap请看看我的更新,谢谢 – 2015-02-09 13:36:12

+0

我该如何做第三个选项? – pap 2015-02-09 16:04:58

0

首先,你可以补充一点:

// This maps the tag of your button to an Intent to launch a new Activity 
private final Map<String,Intent> map = new HashMap<String,Intent>(); 
// A global listener for your buttons 
private final View.OnClickListener listener = new View.OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     String tag = (String) v.getTag(); 
     Intent i = map.get(tag); 
     try{ 
      startActivity(i); 
     }catch(ActivityNotFoundException e){ 
      // Log your exception 
     } 
    } 
}; 

然后,这一点:

// This is the method where you create a new button 
void createNewButton(String tag,Intent i){ 
    Button button = new Button(this); 
    // blah blah for adding it programatically to the layout 
    button.setTag(tag); 
    // add the intent for starting the Activity associated 
    // with this Button to the Map 
    map.put(tag,intent); 
    // add the listener 
    button.setOnClickListener(listener); 
} 

我已经加入了足够的注释使代码不言自明。

如果您可以使用Long标记做成,那么您可以使用SparseArray并由于HashMap而省去一些额外的内存。

+0

我没有看到String标签。我的意思是要调用什么字符串。 – pap 2015-02-09 14:02:21

+0

@pap它将是一个唯一标识每个按钮的字符串。你必须拿出该字符串:) – 2015-02-09 16:30:47

+0

我试过你的代码,但没有出现按钮。我实际上对如何使用它有点困惑。你能给我一些解释吗?比如当我再次参加此活动时按钮将会在那里 – pap 2015-02-09 17:02:02