数据从Intent到String数组索引

时间:2013-04-27 17:51:45

标签: android android-intent android-button

我已将按钮的onClickListener设置为打开另一个活动(片段部分dint工作)并提示用户在edittext框中输入一个数字。按钮Okay,关闭该活动并将结果传递给主活动。然后,数据,即已经提取的数字设置字符串数组的数组索引。知道怎么做吗?

主要活动

public class Quotes extends Activity {

    ImageButton next, copytext, previous, kahinbhi;

    TextView q, tview1, nm;

    String[] str;
    int i,value=0;

    ...
        ...

kahinbhi.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                startActivityForResult(kahinsebhi,value);

            }
        });

    }




    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
         if (requestCode == value) {
                // Make sure the request was successful
                if (resultCode == RESULT_OK) {


                    q.setText(str[data]);



                }
            }


    }


             .....
......

新活动的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/kahinbhiedit"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />

        <requestFocus />

    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="@string/ok" />

</RelativeLayout>

新活动的Java代码

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class kahinsebhii extends Activity {
    Button ok;
    EditText number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kahinsebhii);

    }

}

2 个答案:

答案 0 :(得分:0)

已更新:

EditText editText = (EditText)findViewById(R.id.editText1);

在ClassA中:

Intent intent = new Intent(this, ClassB);
intent.putExtra("string", editText.getText().toString());
startActivity(intent);

在ClassB中:

public void onCreate() {
 Intent intent = getIntent();
 String myString = intent.getStringExtra("string");
}

答案 1 :(得分:0)

做这个

Intent i=new Intent(MainActivity.this,Activity2.class);
                String[]  myString=new  String[]{"editText.getText().toString()"};
                i.putExtra("Strings",myString);
                startActivity(i);

Intent intent=getIntent();
        String[] myString2=intent.getStringArrayExtra("strings");