我的Android应用程序有一点问题。我想在AutoCompleteTextView中添加一个单词,然后跳转到特定的Activity(取决于用户提供的单词)。
问题在于,当我给出一个特定的单词时,程序与预期的答案不对应,但返回错误的toast消息。然而,我把日志,我看到了正确的答案。我认为解决方案很愚蠢,但我坚持并需要解决这个问题。
代码:
Activity.java
public class paralActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paral);
final String [] temp = {"one","two","three"};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.clearListSelection();
final RelativeLayout myRelative = (RelativeLayout) findViewById(R.id.find);
myRelative.setVisibility(View.INVISIBLE);
ImageView myImage = (ImageView) findViewById(R.id.aktoploika);
myImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(paralActivity.this, topParalActivity.class);
startActivity(intent);
}
});
ImageView myOtherImage = (ImageView) findViewById(R.id.aeroporika);
myOtherImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myRelative.setVisibility(View.VISIBLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(paralActivity.this, android.R.layout.select_dialog_item, temp);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
ImageView findBeach = (ImageView) findViewById(R.id.find_beach);
findBeach.setOnClickListener(new View.OnClickListener() {
@Override
** public void onClick(View view) {
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
String choice = actv.getText().toString();
Log.i("Answer",choice);
if (choice == "one"){
Intent firstIntent = new Intent(paralActivity.this, nauagioActivity.class);
startActivity(firstIntent);
}else if (choice == temp[1]){
Intent secondIntent = new Intent(paralActivity.this, gerakasActivity.class);
startActivity(secondIntent);
}else if (choice == temp[2]){
Intent thirdIntent = new Intent(paralActivity.this, limnionasActivity.class);
startActivity(thirdIntent);
}else{
Toast.makeText(paralActivity.this,"wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
为了帮助您理解,在此活动中有2个imageViews。当用户按下第二个时,会出现一个relativeLayout(带有AutoCompleteTextView和一个按钮)。用户在按下按钮后写入单词后,必须转到特定活动。我声明了一个字符串数组(temp [3]),里面有三个单词,每个单词有3个活动。
问题始于我放**的最后一个onclick方法。每次我从数组中输入正确的单词时,我都会收到Toast消息,但在日志中我看到了正确的信息。
这里是Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_paral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main"
android:scaleX="2"
tools:context="com.example.billy.zakynthosapp.paralActivity">
<TextView
android:id="@+id/categories"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/welcome"
android:text="@string/categories"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="11sp"
android:textStyle="bold"
android:textColor="@color/welcome"
android:textAllCaps="true"/>
<ImageView
android:id="@+id/aktoploika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="@id/categories"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="@drawable/par1" />
<TextView
android:id="@+id/aktoploika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/aktoploika"
android:layout_alignTop="@+id/aktoploika"
android:layout_alignRight="@+id/aktoploika"
android:layout_alignBottom="@+id/aktoploika"
android:layout_margin="1dp"
android:gravity="center"
android:text="@string/paralies"
android:textSize="22sp"
android:textColor="@color/categories"
android:textStyle="bold"/>
<ImageView
android:id="@+id/aeroporika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="@id/aktoploika"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="@drawable/par2" />
<TextView
android:id="@+id/aeroporika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/aeroporika"
android:layout_alignTop="@+id/aeroporika"
android:layout_alignRight="@+id/aeroporika"
android:layout_alignBottom="@+id/aeroporika"
android:layout_margin="1dp"
android:gravity="center"
android:text="@string/search"
android:textSize="22sp"
android:textColor="@color/categories"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/aeroporika"
android:layout_marginTop="50dp"
android:layout_centerInParent="true"
android:background="@color/categories"
android:scaleX="0.5">
<TextView
android:id="@+id/textView_1"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:text="@string/find_paral"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/find_beach"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:src="@drawable/find"
android:onClick="find"/>
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView_1"
android:layout_marginTop="10dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
任何人都可以帮我吗? 非常感谢!
答案 0 :(得分:0)
尝试使用choice.equals("one")
代替choice == "one"