当我单击列表视图的按钮时,如何进入其他活动?

Posted

技术标签:

【中文标题】当我单击列表视图的按钮时,如何进入其他活动?【英文标题】:How can i go to an other activity when i click button of list view? 【发布时间】:2021-02-07 22:05:28 【问题描述】:

我想要 Intent 两个不同的活动(FreeLine、MoveCircle)

如果我单击该开始按钮,它将始终启动 FreeLine

如何分离这些意图..?

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        final Context context = parent.getContext();

        if (convertView == null) 
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(mLayout, parent, false);
        

        ImageView img = (ImageView)convertView.findViewById(R.id.img);
        img.setImageResource(mDatas.get(position).Img);

        TextView txt = (TextView)convertView.findViewById(R.id.text);
        txt.setText(mDatas.get(position).Name);

        TextView txt2 = (TextView)convertView.findViewById(R.id.desc);
        txt2.setText(mDatas.get(position).Des);

        Button btn = (Button)convertView.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                //How to separate these two intents???
                Intent FreeLineIntent = new Intent(v.getContext(), FreeLine.class);
                mContext.startActivity(FreeLineIntent);
                Intent MoveCircleIntent = new Intent(v.getContext(), MoveCircle.class);
                mContext.startActivity(MoveCircleIntent);
            
        );
        return convertView;
    

【问题讨论】:

您的问题不清楚。您要在此代码中打开不同的活动?你想达到什么目的? 【参考方案1】:

您可以通过获取文本来做到这一点:-

Button btn = (Button)convertView.findViewById(R.id.btn);
                btn.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        //How to separate these two intents???
                        String txtTitle = txt2.getText().toString();
                        switch (txtTitle)
                            case "the first title  of your text":
                                Intent FreeLineIntent = new Intent(v.getContext(), FreeLine.class);
                                mContext.startActivity(FreeLineIntent);
                                break;
                            case "the second title  of your text":
                                Intent MoveCircleIntent = new Intent(v.getContext(), MoveCircle.class);
                                mContext.startActivity(MoveCircleIntent);
                                break;
                                
                        
                       
                      
                    
                );

【讨论】:

【参考方案2】:
***Button btn = (Button)convertView.findViewById(R.id.btn);
                btn.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        //How to separate these two intents???
                        String txtTitle = txt2.getText().toString();
                        switch (txtTitle)
                            case "the first title  of your text":
                                Intent FreeLineIntent = new Intent(v.getContext(), FreeLine.class);
                                mContext.startActivity(FreeLineIntent);
                                break;
                            case "the second title  of your text":
                                Intent MoveCircleIntent = new Intent(v.getContext(), MoveCircle.class);
                                mContext.startActivity(MoveCircleIntent);
                                break;
                                
                        
                       
                      
                    
                );***

【讨论】:

以上是关于当我单击列表视图的按钮时,如何进入其他活动?的主要内容,如果未能解决你的问题,请参考以下文章

单击按钮时的列表视图

如何从简单列表视图的按钮打开新活动

如何在android中的活动开始时禁用按钮的单击事件检查

在列表视图中添加收藏按钮

当我单击列表视图的操作项时,将文本从列表视图传递到另一个活动

单击列表视图时在活动中显示不同的信息