使用Button Click将TextField String转换为另一个Class'String变量

时间:2014-02-04 21:31:43

标签: android android-intent textfield

我有一个包含两个文本字段和一个按钮的类。如何将文本字段数据转换为另一个类的变量,并在新类的活动中显示它。

我的NewProductActivity类有两个文本字段和一个按钮;

public class NewProductActivity extends Activity {


// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
Button button1;

// url to create new product
private static String url_create_product = "http://XXX";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_product);

    // Edit Text
    inputName = (EditText) findViewById(R.id.inputName);
    inputPrice = (EditText) findViewById(R.id.inputPrice);
            button1 = (Button) findViewById(R.id.button1);
    strName = inputName.getText().toString();


        btnNewProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Launching create new product activity
            Intent i = new Intent(getApplicationContext(), Custom.class);
                    i.putExtra("STRING_VALUE", strName);       
            startActivity(i);

        }

       });

我应该在onClick方法中包含什么来获取此类的两个文本字段变量并将它们分配到Custom类中的两个String变量中?

我还没有在Custom类中写任何东西,你能给我一个Custom类的例子吗?

public class Custom extends Activity{

TextView tw;
String urString;
Bundle extras;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom);
    if (savedInstanceState == null) {
        extras = getIntent().getExtras();

        if(extras == null) {
           urString= null;
        } else {
            urString= extras.getString("STRING_VALUE");
        }     
    } else {
        urString= (String) savedInstanceState.getSerializable("STRING_VALUE");
    }
    tw=new TextView(this); 
    tw=(TextView)findViewById(R.id.tw); 
    tw.setText(urString);

}


private final Runnable mUpdateUITimerTask = new Runnable() {
    public void run() {
        // do whatever you want to change here, like:
        tw.setText(urString);
    }
};
private final Handler mHandler = new Handler();
}

2 个答案:

答案 0 :(得分:1)

here for official docs

您需要在启动意图时使用putExtra方法,并在另一个活动中使用来自Bundle的getExtras

来自vogella.com

的示例
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo"); 

和第二项活动

Bundle extras = getIntent().getExtras();
if (extras == null) {
    return;
    }
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
  // do something with the data
} 

编辑:

我刚才意识到,你应该在你的AndroidManifest.xml中有这个,其中android:name是你的包和类的名称

<activity
    android:name="com.example.stackoverflow.Custom"
    android:label="@string/title_activity_custom" >
</activity>

现在您要在类中插入值

private EditText inputName;
private EditText inputPrice;
private Button button1;

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

    inputName = (EditText) findViewById(R.id.inputName);
    inputPrice = (EditText) findViewById(R.id.inputPrice);
    button1 = (Button) findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
    // Launching create new product activity
        Intent i = new Intent(getApplicationContext(), Custom.class);
        i.putExtra("Name", inputName.getText().toString());     
        i.putExtra("Price", inputPrice.getText().toString());

        startActivity(i);
        //Finish activity where values were inserted if you dont want to come back with back button
        finish();
    }
});

在您想要检索值的课程中

public class Custom extends Activity {

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

        TextView tv = (TextView)findViewById(R.id.text);

        String name = getIntent().getExtras().getString("Name");
        String price = getIntent().getExtras().getString("Price");
        tv.setText(name + " " + price);
}

当然我创建了custom.xml布局,但只是TextView显示了你的值

答案 1 :(得分:1)

传递值:

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
i.putExtra("STRING_VALUE", strName);

要检索尝试类似:

String urString;

if (savedInstanceState == null) {
    extras = getIntent().getExtras();

    if(extras == null) {
       urString= null;
    } else {
        urString= extras.getString("STRING_VALUE");
    }     
} else {
    urString= (String) savedInstanceState.getSerializable("STRING_VALUE");
}