繁体   English   中英

使用onClickListener进行Android启动活动?

[英]Android start activity with onClickListener?

我正在做一个家庭更换申请。 我正在尝试将OnClickListener添加到带有Java的按钮,但我尝试的方式会产生错误:

方法startActivity(Intent)未定义类型new View.OnClickListener(){}

此代码位于适配器MyPagerAdapter

这就是我想要的:

    buttonItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("com.android.contacts.ContactsApplication");
            startActivity(intent);
        }
    });

如何将OnClickListener添加到打开另一个应用程序的按钮,例如com.android.contacts.ContactApplication


编辑:这是完整的代码,我现在正在尝试:

public class MyPagerAdapter extends PagerAdapter {

    @Override
    public Object instantiateItem(View container, int position) {
        Context context = container.getContext();
        LinearLayout layout = new LinearLayout(context);
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        TextView textItem = new TextView(context);
        Button buttonItem = new Button(context);
        buttonItem.setText("Aceptar");

        // This is what I'm trying, (crashes on click)
        buttonItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.android.contacts.ContactsApplication");
               v.getContext().startActivity(intent); 
            }
        });
 buttonItem.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent();
        i.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity"));
        i.setAction("android.intent.action.MAIN");
        i.addCategory("android.intent.category.LAUNCHER");
        i.addCategory("android.intent.category.DEFAULT");
        v.getContext().startActivity(i);
    }
(findViewById(R.id.button)).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(v.getContext(), ACTIVITY.class));
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM