安卓:startActivityForResult和放大器;的setResult一个视图类和活动类放大器、视图、startActivityForResult、setResult

2023-09-12 05:15:20 作者:茫茫人生好像荒野

我很迷茫,不知道如何使用startActivityResults和setResults获得由previous活动数据。我有一个观点类和活动类。

基本上在我看来,我班有这样的对话,它会真正开始叫colorActivity类的活动类。当用户选择是也将传递选定圆的colorActivity类的名称。在colorActivity类,允许用户输入的颜色code代表一个特定的圈子,我想通过颜色code回视图类。我从后面的活动传递值的问题,查看使用startActivityForResult和的setResult方法。添加上,如何利用所获取的数据afterthat的?

我的code如下:

的onTouchEvent code,从我的观点类:

  @覆盖
            公共布尔的onTouchEvent(MotionEvent事件){

                X = event.getX();
                Y = event.getY();


                开关(event.getAction()){
                案例MotionEvent.ACTION_DOWN:


                    的for(int i = 0; I< circles.size();我++){


                        如果(circles.get(ⅰ)。载(X,Y)){
                            circleID =我;

            处理程序处理程序=新的处理程序();
                                handler.postDelayed(新的Runnable(){
                                    @覆盖
                                    公共无效的run(){
                                        AlertDialog.Builder建设者=新生成器(
                                                的getContext());
                                        最后的EditText文本=新的EditText(的getContext());

                                        builder.setTitle(添加颜色以圈)。setMessage(
                                                继续输入色);
                                        builder.setPositiveButton(是,
                                                新DialogInterface.OnClickListener(){

                                                    公共无效的onClick(DialogInterface迪,
                                                            int i)以{

                                                        意向意图=新的意图(
                                                                的getContext(),
                                                                colorActivity.class);

                                                         intent.putExtra(circlename,circleNameList.get(circleID));


    startActivityForResults(意向,1); //错误这里发生:该方法startActivityForResult(意向,INT)是未定义的类型新DialogInterface.OnClickListener(){}
                                                    }

                                                });
                                        builder.setNegativeButton(否,
                                                新DialogInterface.OnClickListener(){

                                                    公共无效的onClick(DialogInterface迪,
                                                            int i)以{
                                                    }

                                                });

                                        builder.create()显示()。
                                    }
                                },3000);
    打破;

    }

    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == 1){//请使用最终诠释,而不是硬codeD
                                // int值
            如果(结果code == RESULT_OK){
                 C code =(字符串)data.getExtras()的getString(色code)。
        }

        }
    }

公共静态字符串的getColor code(){
        回复C code;
    }
 

在colorActivity:

 保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.add_ecolor);


        circlenametextview =(TextView中)findViewById(R.id.circlenametextview);


        串circlename = super.getIntent()getStringExtra(circlename);
          circlenametextview.setText(circlename); //获取圈子名称


savebutton.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){

                 意向意图=新的意图(colorActivity.this,?????); //如何返回到视图类?


               彩code = colorEditText.getText()的toString(); //我能得到正确的价值直至这一点
              意图resultIntent =新意图();
                   resultIntent.putExtra(色code,颜色code);

                   的setResult(Activity.RESULT_OK,resultIntent);
                   完();
            } //的onclick

        });
        }
 
音频放大器

解决方案

在纠正对方code,这样就可以运行这个程序,你可以检索参数从活动回来 colorActivity 以这种方式:

第一步:从colorActivity返回一些值

 意图resultIntent =新意图();
resultIntent.putExtra(参数名,valueOfParameter);
...
的setResult(Activity.RESULT_OK,resultIntent);
完();
 

步骤2:从主要活动收集的数据

重写 @onActivityResult(...)

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == 1){//请使用最终诠释,而不是硬codeD int值
    如果(结果code == RESULT_OK){
        字符串值=(字符串)data.getExtras()的getString(参数名)。
 

引用

http://developer.android.com/training/basics/intents/result.html Android,如何管理启动活动的结果? http://steveliles.github.io/returning_a_result_from_an_android_activity.html

I am confused and have no idea on how to use the startActivityResults and setResults to get data from previous activity. I have a view class and a activity class.

Basically in my view class i have this dialog and it will actually start the activity class called the colorActivity class. When user selects yes also it will pass the name of the selected circle to the colorActivity class. At the colorActivity class, users are allowed to enter color code for a particular circle and i would like to pass the color code back to the view class. I have problems passing values from activity back to view using the startActivityForResult and setResult method. Adding on, how to make use of the fetched data afterthat?

my code are as follows

Ontouchevent code from my view class:

            @Override
            public boolean onTouchEvent(MotionEvent event) {

                x = event.getX();
                y = event.getY();


                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:


                    for (int i = 0; i < circles.size(); i++) {


                        if (circles.get(i).contains(x, y)) {
                            circleID = i;

            Handler handler = new Handler();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        AlertDialog.Builder builder = new Builder(
                                                getContext());
                                        final EditText text = new EditText(getContext());

                                        builder.setTitle("Adding colors to circles").setMessage(
                                                "Proceed to Enter color");
                                        builder.setPositiveButton("Yes",
                                                new DialogInterface.OnClickListener() {

                                                    public void onClick(DialogInterface di,
                                                            int i) {

                                                        Intent intent = new Intent(
                                                                getContext(),
                                                                colorActivity.class);

                                                         intent.putExtra("circlename", circleNameList.get(circleID));


    startActivityForResults(intent, 1); // error incurred here : The method startActivityForResult(Intent, int) is undefined for the type new DialogInterface.OnClickListener(){}
                                                    }

                                                });
                                        builder.setNegativeButton("No",
                                                new DialogInterface.OnClickListener() {

                                                    public void onClick(DialogInterface di,
                                                            int i) {
                                                    }

                                                });

                                        builder.create().show();
                                    }
                                }, 3000);
    break;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) { // Please, use a final int instead of hardcoded
                                // int value
            if (resultCode == RESULT_OK) {
                 ccode = (String) data.getExtras().getString("colorcode");
        }

        }
    }

public static String getColorCode() {
        return ccode;
    }

In the colorActivity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_ecolor);


        circlenametextview = (TextView)findViewById(R.id.circlenametextview);


        String circlename = super.getIntent().getStringExtra("circlename");
          circlenametextview.setText(circlename);//get the circle name


savebutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                 Intent intent = new Intent(colorActivity.this, ?????);//how to return back to the view class?


               colorcode = colorEditText.getText().toString();// I am able to get value right up till this point
              Intent resultIntent = new Intent();
                   resultIntent.putExtra("colorcode", colorcode );

                   setResult(Activity.RESULT_OK, resultIntent);
                   finish();
            }// onclick

        });
        }

解决方案

After correcting the other code so that you can run the program, you can retrieve parameters back from your activity colorActivity in this way:

Step1: return some value from colorActivity

Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();

Step 2: collect data from the Main Activity

Overriding @onActivityResult(...).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { // Please, use a final int instead of hardcoded int value
    if (resultCode == RESULT_OK) {
        String value = (String) data.getExtras().getString("NAME OF THE PARAMETER");

References

http://developer.android.com/training/basics/intents/result.html Android, How to manage start activity for result? http://steveliles.github.io/returning_a_result_from_an_android_activity.html
 
精彩推荐