Android 将活动内容带到对话框

Android 将活动内容带到对话框,android,android-dialog,Android,Android Dialog,我想创建一个自定义对话框,在该对话框中,我想按“是”按钮拨打电话,按“否”按钮取消对话框。但在对话框中的文本视图中,我希望确认对话框显示“呼叫:号码”。我已从主活动中的用户处获取此“否”,无法将此“否”带到对话框中。我该怎么做? 我曾试图通过意向和共同的偏好,但都是徒劳的。给我一些建议 我在主活动类中编写了以下代码 package com.example.deepak.phone_call; import android.Manifest; import android.app.Activit

我想创建一个自定义对话框,在该对话框中,我想按“是”按钮拨打电话,按“否”按钮取消对话框。但在对话框中的文本视图中,我希望确认对话框显示“呼叫:号码”。我已从主活动中的用户处获取此“否”,无法将此“否”带到对话框中。我该怎么做? 我曾试图通过意向和共同的偏好,但都是徒劳的。给我一些建议

我在主活动类中编写了以下代码

package com.example.deepak.phone_call;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    Button b;
    EditText edittext1;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor edit;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.button);
        b.setOnClickListener(this);
        edittext1=(EditText)findViewById(R.id.edittext1);
       String number=edittext1.getText().toString();
        sharedPreferences = getSharedPreferences("mydata",MODE_PRIVATE);
        edit= sharedPreferences.edit();
                edit.putString("s1",number);
        edit.commit();

       // Intent intent = new Intent(this, phone_call.class);
       // intent.putExtra("s1",number);

    }
    private void phoneDialog(){

        Dialog dialog = new Dialog(MainActivity.this);

        dialog.setContentView(R.layout.phone_call);

        dialog.setTitle("Call:");

        dialog.show();

    }

        @Override
    public void onClick(View v) {
           phoneDialog();

     }
 }
下面是我在另一个类中设置对话框的代码

    import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Deepak on 7/5/2016.
 */
public class phone_call extends Activity implements View.OnClickListener {
Button button2,button3;
    TextView editText;
    SharedPreferences shredpreference;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phone_call);
        button2= (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3= (Button) findViewById(R.id.button3);
        button3.setOnClickListener(this);

        editText = (TextView) findViewById(R.id.editText);
        //Intent intent = getIntent();
       //String s1= intent.getStringExtra("s1");

shredpreference = getSharedPreferences("mydata",0);
      String s1=  shredpreference.getString("s1","null");
        editText.setText("Call"+ s1+":");

    }

    @Override
    public void onClick(View v) {
        //Intent intent = getIntent();
       // String s1= intent.getStringExtra("s1");
        shredpreference = getSharedPreferences("mydata",0);
        String s1=  shredpreference.getString("s1","null");

        switch (v.getId()) {
            case R.id.button2:
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" +s1 ));//change the number


                try{
                    startActivity(callIntent);
                }

                catch (android.content.ActivityNotFoundException ex){
                    Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button3:
                finishActivity(100);
                break;
        }

    }
}

您将用于存储值的代码放在了错误的位置,因此它不会像oncreate()方法那样在任何时候获取值

因此,从onCreate()方法中删除下面两行,并按如下所示在onCLick()方法中写入它。

     @Override
     public void onClick(View v)
     {
          String number = edittext1.getText().toString();
          edit.putString("s1",number);
          edit.commit();

          phoneDialog();

     }