【问题标题】:Android Button Onclick listener not workingAndroid Button Onclick 监听器不工作
【发布时间】:2014-11-09 06:12:44
【问题描述】:

我的活动有两个按钮。一个工作完美,一个没有。我似乎找不到问题所在。这是代码。 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。

标签: android android-activity onclicklistener android-button


【解决方案1】:

你好,为什么不显示吐司。吐司代码应该是这样的。

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

【讨论】:

  • 非常感谢。我没看到。小错误
  • 继续努力 :) 不错的想法
【解决方案2】:

您可以尝试创建一个 OnClickListener 变量,然后将两个按钮设置为同一个变量,并使用如下 switch case 语句将它们分开:

OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add_contact:
             //Do what you want for select_contact
            break;
        case R.id.create_button:
            //Do what you want for create_button
            break;
        default:
            break;
        }


    }
};

在您的 OnCreate 方法中设置如下:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        select_contact = (Button)findViewById(R.id.add_contact);
        create_profile = (Button)findViewById(R.id.create_button);
        select_contact .setOnClickListener(onClickListener);
        create_profile .setOnClickListener(onClickListener);

}

如果你想检查它,只需在 case R.id.create_button

行下添加一个断点

即使这没有解决您的问题,我相信使用 onClickListener 方法可以更轻松、更舒适地工作:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多