按钮的 onClick() 没有被调用

Button's onClick() not getting called

我正在尝试创建一个呼叫按钮,当用户点击一个按钮时,它会进行 phone 呼叫。

现在我有了单选按钮和该按钮的代码,但是单击它时什么也没做。

我已经在清单中设置了 CALL_PHONEREAD_PHONE_STATE 权限,但按下按钮后仍然无法正常工作。

这是 activity 中单选按钮的代码:

Button radioButton;
call();


radioButton = (Button) findViewById(R.id.radioButton);
}

private void call()
{
    Intent in = new Intent(Intent.ACTION_DIAL,Uri.parse("0000000000"));
    try{
        startActivity(in);
    }
    catch (android.content.ActivityNotFoundException ex)
    {
        Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
    }
}

创建 RadioGroup,然后在 RadioGraoup

中创建 RadioButton
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton callRadioButton=(RadioButton)findViewById(checkedId);

        //gettext you write with radiobutton
        String  dailString=callRadioButton.getText().toString();

        //check if you select the radio button
        if(dailString.trim().equals("dailnumber"){
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:0123456789")); 
            startActivity(intent); 
        }else{ 
        // do action with other android button
        }
    } 
});

已编辑

java代码

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.btn_radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton callRadioButton=(RadioButton)findViewById(checkedId);

                //gettext you write with radiobutton
                String  dailString=callRadioButton.getText().toString();

                //check if you select the radio button
                if(dailString.trim().equals("Dail")){
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:0123456789"));
                    startActivity(intent);
                }else{
                    // do action with other android button
                }
            }
        });
    }
}



***XML***

迷你节

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radiogroupPermistion">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

XML'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:id="@+id/btn_radio_group"
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dail"
            android:layout_weight="1"
            android:id="@+id/dail_id"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/otherid"
            android:layout_weight="1"

            />
    </RadioGroup>
</LinearLayout>
</LinearLayout>

编辑 2

按钮监听器

java代码

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button dailnumber = (Button) findViewById(R.id.btn_call_number);
    dailnumber.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
      Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:0123456789"));
                    startActivity(intent);     }});
  }}

首先,您没有在单击单选按钮时调用 Call 方法。这是您的问题的解决方法。

第一种方式:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
               call();
} });

然后像这样调用你的方法

private void call(){
    Intent in = new Intent(Intent.ACTION_DIAL,Uri.parse("0000000000"));
    try{
        startActivity(in);
    }
    catch (android.content.ActivityNotFoundException ex)
    {
        Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
    }
}

第二种方式:

Button myButton=(Button)findViewById(R.id.buttonId);

myButton.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v){
       call();     
    }
});