单击片段内的按钮时如何执行操作

时间:2018-10-16 15:40:32

标签: android android-studio fragment android-context

在片段中我有一个按钮,当我按下按钮时,我想执行通话。

但是,当我单击按钮时,似乎什么都没发生,这是我在做什么错?

这是我的片段代码:

package fr.aitec_informatique.aitecinformatiques;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.content.Context;
import android.widget.ListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    Button dial;

    private Context mContext;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_home, container, false);

        v.findViewById(R.id.dial);

        return v;
    }
    public void onDialButton(View v) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:xxxxxxxxxx"));

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        startActivity(intent);
    }
}

2 个答案:

答案 0 :(得分:0)

像这样初始化Button

dial = v.findViewById(R.id.dial);

可能是您没有正确定义ButtongetActivity()实际上有效。

此外,onDialButton从未使用过。而是将监听器添加到Button中,然后调用onDialButton

dial.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

      onDialButton();
    }
});

更新:

您现在应该使用ContextCompat.checkSelfPermission

public void onDialButton() {

    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted
    }
    // first check for the permission

    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:xxxxxxxxxx"));
    startActivity(intent);
}

然后只需在您的onDialButton()中呼叫onClick()

阅读文档:https://developer.android.com/training/permissions/requesting#java

答案 1 :(得分:0)

我认为您使用的不是普通的OnClickListener

尝试一下:

public class HomeFragment extends Fragment {

Button dial;

private Context mContext;

public HomeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_home, container, false);

    dial = (Button)v.findViewById(R.id.dial);
      //I think dial is the id of your button

    dial.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        onDialButton();
    }
});

    return v;
}
public void onDialButton() {
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:xxxxxxxxxx"));

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    startActivity(intent);
 }
}