我基本上是在尝试创建一个包含 2 个活动的应用程序。在活动 1 中,用户可以在 2 个单独的编辑文本字段中输入 2 个值,我提供了一个微调器下拉项,用户可以使用它选择 2 个值中的任何一个。
如果用户选择选项为 1 并单击提交按钮,用户将显示第二个活动,其值将根据在先前活动中微调对象中选择的 id 显示。
就像如果用户选择 1 ,则显示第一个编辑文本框中的值,如果他选择 2 ,则显示第二个编辑文本框的值。
此外,当用户单击第二个活动中的提交按钮时,根据他在第二个活动的编辑文本字段中输入的值,它将显示在上一个活动的编辑文本框 1 或编辑文本框 2 中。
我无法理解为什么我的应用程序显示出奇怪的行为。请指导我。
第一个活动文件:
package spinnner.intents.understand;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class TwoAppsActivity extends Activity {
/** Called when the activity is first created. */
String [] items = {"1","2"};
//String ch;
TextView tv;
EditText et1,et2;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.txt);
et1=(EditText) findViewById(R.id.edittxt1); // value 1
et2=(EditText) findViewById(R.id.edittxt2); // value 2
spinner=(Spinner) findViewById(R.id.spin1); // select
//
Intent i3 = getIntent();
if(i3.getStringExtra("spinner.intents.understand.value1")!=null)
{
et1.setText(i3.getStringExtra("spinner.intents.understand.value1"));
}
else if(i3.getStringExtra("spinner.intents.understand.value2")!=null)
{
et2.setText(i3.getStringExtra("spinner.intents.understand.value2"));
}
else
{
Toast.makeText(getApplicationContext(), "Enter some text ", Toast.LENGTH_LONG).show();
}
// spinner logic
ArrayAdapter<String> as1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);
as1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(as1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
tv.setText(items[arg2]);
Toast.makeText(getApplicationContext(), " You selected : "+items[arg2],Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You selected nothing", Toast.LENGTH_LONG).show();
}
});
//
}
public void Submit(View v)
{
Intent i = new Intent(this,Second.class);
startActivity(i);
//str1 = et1.getText().toString();
//Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();
// str2 = et2.getText().toString();
//Toast.makeText(getApplicationContext(), str2, Toast.LENGTH_LONG).show();
//
if(spinner.getSelectedItem().toString()=="1")
{
Toast.makeText(getApplicationContext(), "I am in 1st option", Toast.LENGTH_LONG).show();
i.putExtra("spinner.intents.understand.value1", et1.getEditableText().toString());
i.putExtra("spinner.intents.understand.Id1", "1");
}
else if(spinner.getSelectedItem().toString()=="2")
{
Toast.makeText(getApplicationContext(), "I am in 2nd option", Toast.LENGTH_LONG).show();
i.putExtra("spinner.intents.understand.value2", et2.getEditableText().toString());
i.putExtra("spinner.intents.understand.Id2", "2");
}
else
{
Toast.makeText(getApplicationContext(), "You selected nothing!!!", Toast.LENGTH_LONG).show();
}
}
}
第二个活动文件
package spinnner.intents.understand;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Second extends Activity{
/** Called when the activity is first created. */
TextView Tv;
EditText Et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//
Tv=(TextView) findViewById(R.id.txtS1);
Et=(EditText) findViewById(R.id.edittxtS1);
//
Intent i1 = getIntent();
if((i1.getStringExtra("spinner.intents.understand.Id1"))=="1")
{
Toast.makeText(getApplicationContext(), "I am in value 1", Toast.LENGTH_LONG).show();
Et.setText(i1.getStringExtra("spinner.intents.understand.value1"));
}
else if((i1.getStringExtra("spinner.intents.understand.Id2"))=="2")
{
Toast.makeText(getApplicationContext(), "I am in value 2", Toast.LENGTH_LONG).show();
Et.setText(i1.getStringExtra("spinner.intents.understand.value2"));
}
else
{
Toast.makeText(getApplicationContext(), "Some mismatch", Toast.LENGTH_SHORT).show();
}
}
public void submit(View v)
{
Intent i2 = new Intent(this,TwoAppsActivity.class);
startActivity(i2);
if(i2.getStringExtra("Id1")=="1")
{
i2.putExtra("spinner.intents.understand.value1", Et.getEditableText().toString());
}
else if(i2.getStringExtra("Id2")=="2")
{
i2.putExtra("spinner.intents.understand.value2", Et.getEditableText().toString());
}
else
{
Toast.makeText(getApplicationContext(), "No data to display", Toast.LENGTH_LONG).show();
}
}
}
主文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/txt1"
android:text="Enter value 1:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittxt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt2"
android:text="Enter value 2:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittxt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt3"
android:text="Select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="@+id/spin1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnsubmit1"
android:text="Submit"
android:onClick="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
第二个活动 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtS1"
android:text="Value:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edittxtS1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnsubmit2"
android:text="Submit"
android:onClick="submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
安卓清单 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="spinnner.intents.understand"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TwoAppsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Second">
</activity>
</application>
</manifest>
如果我的代码看起来太乱了,我也愿意邮寄我的项目。