startActivityForResult()不起作用

时间:2015-10-26 15:17:04

标签: java android onclicklistener

我在Claims.java中有一个按钮。按下button后,它会显示Alert Dialog Window radio buttons。如果选中radio button,它将转到特定活动。在活动中,它有一个editText和一个save按钮。当点击活动中的button按钮时,我想在save(Claims.java)上显示editText上的值。

Claims.java中的Claims.java >> AlertDialog窗口>> AlertDialogRadio.java

我使用 startActivityForResult()AlertRadioDialog.java接收结果。但现在的问题是它会显示AlertDialogRadio这不是我想要的,而且textView上没有显示文字。我该怎么做才能实现这个目标?

Claims.java

 public class Claims extends Fragment {
    private TextView c;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View claims = inflater.inflate(R.layout.claims, container, false);


        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialogRadio();

            }
        };
        Button button1 = (Button) claims.findViewById(R.id.button10);
        Button button = (Button) claims.findViewById(R.id.button8);
        button1.setOnClickListener(listener);
         c=(TextView)claims.findViewById(R.id.textView49);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
                startActivity(intent);
            }
        });
        return claims;
    }

    public void AlertDialogRadio() {
        final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
                , "Medical", "Other"};

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
        alt_bld.setTitle("Select a Claims");
        alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                .OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                    startActivity(intent);
                } else if (item == 1) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                    startActivity(intent);
                } else if (item == 2) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
                    startActivity(intent);
                } else if (item == 3) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
                    startActivity(intent);
                } else if (item == 4) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
                    startActivity(intent);
                }

            }
        });
        AlertDialog alert = alt_bld.create();
        alert.show();


    }
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                String result=data.getStringExtra("text");
                c.setText(result);


            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }//onActivityResult
}

假设用户选择Project。

Project1.java

  public class Project1 extends AppCompatActivity {
    private static String text;
    private static EditText txt;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.project);
       txt= (EditText)findViewById(R.id.editText36);
        Button b=(Button)findViewById(R.id.button17);

        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent returnIntent = new Intent();
                text = txt.getText().toString();
                returnIntent.putExtra("text", text);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

首先,您根本没有使用startActivityForResult

以下是您应该如何继续:

<强> Claims.java

public static final int PROJECT_REQUEST_CODE = 1;
public static final int CAMERA_REQUEST_CODE = 2;
public static .....
if (item == 0) {
     Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
     startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
else if .....

OnActivityResult

if (requestCode == PROJECT_REQUEST_CODE) {
...
}
else if(requestCode == CAMERA_REQUEST_CODE) {
...
}
else if ...