Android 按钮 Onclick 监听器不工作

标签 android android-activity onclicklistener android-button

我的 Activity 有两个按钮。一个工作完美,一个不是。我似乎找不到问题所在。这是代码。 create_profile 按钮不起作用。谁能告诉我为什么?

    private Button create_profile,select_contact;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_main);

    select_contact = (Button)findViewById(R.id.add_contact);
    create_profile = (Button)findViewById(R.id.create_button);

    select_contact.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
            Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
            startActivityForResult(intentPickContact, PICK_CONTACT);    
        }
    });

    create_profile.setOnClickListener(new OnClickListener() {

        @SuppressLint("ShowToast")
        @Override
        public void onClick(View arg0) {
            Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG);
            System.out.println("PRessed");

        }
    });
    }

这是xml代码

          <Button 
                    android:id="@+id/add_contact"
                    android:layout_width="85dp"
                    android:layout_height="wrap_content"
                    android:text="Add"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="2dp"
                    android:textSize="19dp"/>
        <Button 
            android:id="@+id/create_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Create Profile"
            android:textSize="18sp"
            android:layout_marginTop="8dp"/>

我已经检查了所有内容,但似乎找不到问题所在。

最佳答案

嗨,你为什么不显示 toast 。 toast 代码应该是这样的。

 Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG).show();

关于Android 按钮 Onclick 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825554/

相关文章:

javascript - 将 HTML 链接添加到 Google map 圆形元素

android - SQLite 行限制

java - 如何将 Android Activity 转换为界面?

android - 是否可以使用 Dexmaker 覆盖现有类中的方法?

java - 如何创建从今天起 X 天结束的 Android 日历事件

android - 警报对话框中使用的 View 内的 onClick 按钮

android - 通过 SQLiteDatabase 进行复杂查询

Android - 如何检测堆栈中的最终 Activity

android - 如何设计像优步应用程序这样的 Activity

java - 如何在android java中通过按钮的ID使用OnClickListener属性