【问题标题】:One onClickListener for multiple TextViews一个用于多个 TextView 的 onClickListener
【发布时间】:2014-12-14 17:22:32
【问题描述】:

我有一个多行的 ListView,每行都有多个 textView,现在为每行设置一个 onClickListener 既不方便又糟糕。

我如何为所有 TextView 设置一个 onClickListener,因为每个都将执行相同的意图(它会进入相同的下一个活动)。但是每个 on 都将其文本作为该意图中的额外内容传递。

那么我该如何为 TextViews 设置这个,点击 Listineres 呢?

感谢您的帮助。

编辑

好的,抱歉,我可能没有为我设置的 ListView 完全解释这一点。

我有 5 种不同类型的细胞。一个有 4 个 textViews,一个有 3 个 textViews,以此类推到 0。

在我的 getView 中,我有这样的设置

switch(stringArrayList.size()){
            case 4:
                convertView = mLayoutInflater.inflate(R.layout.four_meal_item_view, parent, false);
                TextView mealOne_Four = (TextView) convertView.findViewById(R.id.firstMealTextView);
                TextView mealTwo_Four = (TextView) convertView.findViewById(R.id.secondMealTextView);
                TextView mealThree_Four = (TextView) convertView.findViewById(R.id.thirdMealTextView);
                TextView mealFour_Four = (TextView) convertView.findViewById(R.id.fourthMealTextView);
                TextView dayText_Four = (TextView) convertView.findViewById(R.id.dayTextView);
                dayText_Four.setText(dayString);
                mealOne_Four.setText(stringArrayList.get(0).substring(0,1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
                mealTwo_Four.setText(stringArrayList.get(1).substring(0,1).toUpperCase() + stringArrayList.get(1).substring(1).toLowerCase());
                mealThree_Four.setText(stringArrayList.get(2).substring(0,1).toUpperCase() + stringArrayList.get(2).substring(1).toLowerCase());
                mealFour_Four.setText(stringArrayList.get(3).substring(0,1).toUpperCase() + stringArrayList.get(3).substring(1).toLowerCase());
                break;
            case 3:
                convertView = mLayoutInflater.inflate(R.layout.three_meal_item_view, parent, false);
                TextView mealOne_Three = (TextView) convertView.findViewById(R.id.firstMealTextView_Three);
                TextView mealTwo_Three = (TextView) convertView.findViewById(R.id.secondMealTextView_Three);
                TextView mealThree_Three = (TextView) convertView.findViewById(R.id.thirdMealTextView_Three);
                TextView dayText_Three = (TextView) convertView.findViewById(R.id.dayTextView_Three);
                dayText_Three.setText(dayString);
                mealOne_Three.setText(stringArrayList.get(0).substring(0,1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
                mealTwo_Three.setText(stringArrayList.get(1).substring(0,1).toUpperCase() + stringArrayList.get(1).substring(1).toLowerCase());
                mealThree_Three.setText(stringArrayList.get(2).substring(0,1).toUpperCase() + stringArrayList.get(2).substring(1).toLowerCase());
                break;
            case 2:
                convertView = mLayoutInflater.inflate(R.layout.two_meal_item_view, parent, false);
                TextView mealOne_Two = (TextView) convertView.findViewById(R.id.firstMealTextView_Two);
                TextView mealTwo_Two = (TextView) convertView.findViewById(R.id.secondMealTextView_Two);
                TextView dayText_Two = (TextView) convertView.findViewById(R.id.dayTextView_Two);
                dayText_Two.setText(dayString);
                mealOne_Two.setText(stringArrayList.get(0).substring(0,1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
                mealTwo_Two.setText(stringArrayList.get(1).substring(0,1).toUpperCase() + stringArrayList.get(1).substring(1).toLowerCase());
                break;
            case 1:
                convertView = mLayoutInflater.inflate(R.layout.one_meal_item_view, parent, false);
                TextView mealOne_One = (TextView) convertView.findViewById(R.id.firstMealTextView_One);
                TextView dayText_One = (TextView) convertView.findViewById(R.id.dayTextView_One);
                dayText_One.setText(dayString);
                mealOne_One.setText(stringArrayList.get(0).substring(0,1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
                break;
            case 0:
                convertView = mLayoutInflater.inflate(R.layout.zero_meal_item_view, parent, false);
                TextView dayText_Zero = (TextView) convertView.findViewById(R.id.dayTextView_Zero);
                dayText_Zero.setText(dayString + "(Closed)");
                break;

对于单击 textView 不,我想转到一个新活动并传递被单击的 TextView 的文本和该行的 dayTextView 的文本。

我希望这能更好地解释它。

【问题讨论】:

标签: android textview onclicklistener


【解决方案1】:

getView 方法中创建视图时,您可以为适配器中的每个 TextView 创建 OnClickListener

还有第二种选择。使用一个全局 OnClickListener 和....

public void onClick(View view) {
   if(view instanceof YourFunkyCustomTextViewWithData)
   {
       ((YourFunkyCustomTextViewWithData) view).getFunkyData()
        ... and do what you want with that
   }
}

【讨论】:

    【解决方案2】:

    您应该在AdaptergetView 方法中将onClickListener 添加到每个TextView

    编辑:

    为列表中的每个TextView 设置onClickListener。然后在onClick 方法中:

    @Override
     public void onClick(View view) {
         Intent i = new Intent(context, MyActivity.class);
         i.putExtra("text", ((TextView) view).getText.toString());
         context.startActivity(this);
     }
    

    【讨论】:

    • 列表项的视图持有者。如果您没有使用任何视图持有者,请将其替换为 convertView 或您的任何视图。
    • 好吧,如果你有机会看到编辑,我会更好地解释我的问题
    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多