1

我已将按钮的 onClickListener 设置为打开另一个活动(片段部分 dint 工作)并提示用户在编辑文本框中输入一个数字。按钮 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);

    }

}
4

2 回答 2

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");
于 2013-04-27T18:27:20.537 回答
0

更新 :

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

在 A 类:

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

B类:

public void onCreate() {
 Intent intent = getIntent();
 String myString = intent.getStringExtra("string");
}
于 2013-04-27T17:54:56.077 回答