设置OnClickListener泛型方法

时间:2017-04-28 15:22:17

标签: java android generics onclicklistener

我有一项活动,它有不同的TextView。现在,通过单击每个TextView,它应该打开另一个活动。我实现了通用方法,因为我想删除冗余代码。 所以我只是想询问你的意见,如果我以正确的方式在java中使用泛型概念,并且你是否会使用类似的方法将侦听器设置为Android中的多个视图?

(我使用通用名称进行演示,我不想在xml-Layout上使用onClick声明):

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView activity1 = (TextView) findViewById(R.id.activity1);
    TextView activity2 = (TextView) findViewById(R.id.activity2);
    TextView activity3 = (TextView) findViewById(R.id.activity3);
    TextView activity4 = (TextView) findViewById(R.id.activity4);

    setOnClickListener(activity1, Activity1.class);
    setOnClickListener(activity2,  Activity2.class);
    setOnClickListener(activity3,  Activity3.class);
    setOnClickListener(activity4, Activity4.class);
}


private void setOnClickListener (TextView textView, final Class<? extends AppCompatActivity> activityClass){

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent activityIntent = new Intent(MainActivity.this, activityClass);
            startActivity(activityIntent);
        }
    });
}

}

3 个答案:

答案 0 :(得分:1)

我建议覆盖View.OnClickListener。将代码与Activity分离具有将逻辑与Activity本身分离的额外好处,因此您可以在不同的上下文中使用GenericClickListener。  像这样:

public class GenericClickListener implements View.OnClickListener {
    public GenericClickListener(Activity activity, Class<? extends AppCompatActivity> activityClass) {
        @Override
        public void onClick(View v) {
            Intent activityIntent = new Intent(activity, activityClass);
            startActivity(activityIntent);
        }
    } 
}

然后在你的onCreate()方法中:

TextView activity1 = (TextView) findViewById(R.id.activity1);
TextView activity2 = (TextView) findViewById(R.id.activity2);
TextView activity3 = (TextView) findViewById(R.id.activity3);
TextView activity4 = (TextView) findViewById(R.id.activity4);

activity1.setOnClickListener(new GenericClickListener(thia, Activity1.class));
activity2.setOnClickListener(new GenericClickListener(this, Activity2.class));
activity3.setOnClickListener(new GenericClickListener(this, Activity3.class));
activity4.setOnClickListener(new GenericClickListener(this, Activity4.class));

除此之外,您的实施似乎是一种合理的方法。

答案 1 :(得分:0)

如果此代码有效,那么这是一个非常好的概念。 我会喜欢这个

答案 2 :(得分:0)

我正在为此制定自己的解决方案,并且在检查现有元素时找到了该线程。以下代码有效(@RaceYouAnytime答案几乎没有错误),并允许您使用ActivityFragment来实例化StartActivity OnClickListener`

public class StartActivityOnClickListener implements View.OnClickListener
{
    private Activity sourceActivity;
    private Class<?> destinationActivityClass;

    /**
     * Constructor to use in Activities
     * */
    public StartActivityOnClickListener(Activity srcActivity, Class<? extends AppCompatActivity> dstActivityClass)
    {
        this.sourceActivity = srcActivity;
        this.destinationActivityClass = dstActivityClass;
    }

    /**
     * Constructor to use in Fragments
     * */
    public StartActivityOnClickListener(Fragment srcFragment, Class<? extends AppCompatActivity> dstActivityClass)
    {
        this.sourceActivity = srcFragment.getActivity();
        this.destinationActivityClass = dstActivityClass;
    }

    /**
     * Overridden onClick() method building the Intent and calling startActivity() with it
     */
    @Override
    public void onClick(View v)
    {
        sourceActivity.startActivity(new Intent(sourceActivity, destinationActivityClass));
    }
}

用法保持不变,如下所示

yourObject.setOnClickListener(new StartActivityOnClickListener(this, YourActivity.class));