我尽力解决问题,希望有人能帮助我找到解决方案。
我为我的活动制作了以下布局:
Iterator<String> iter = a.iterator();
while (iter.hasNext()) {
String str = iter.next();
a = new ArrayList<>();
System.out.println(str);
}
相关的java代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@android:color/holo_orange_light"
android:visibility="visible"
tools:context="com.example.youssef.mylocation.Main2Activity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/holo_orange_dark"
android:text="@string/menumessage"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText1"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="128dp"
android:ems="10"
android:hint="@string/phone_number"
android:inputType="phone"
android:textColor="@color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="196dp"
android:ems="10"
android:hint="@string/address"
android:inputType="textPersonName"
android:textColor="@android:color/holo_orange_dark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="@string/place_name"
android:inputType="textPersonName"
android:textColor="@color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="340dp"
android:textColor="@color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="24dp"
android:textColor="@color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/CancelBtn"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginBottom="40dp"
android:layout_marginStart="32dp"
android:background="@android:color/holo_red_dark"
android:text="@string/cancel"
android:onClick="Cancelonbuttonclickfunc"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/city"
android:inputType="textPersonName"
android:textColor="@color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
<TextView
android:id="@+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="144dp"
android:text="Required !"
android:textColor="@android:color/holo_red_dark"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="@+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/AddBtn"
android:layout_width="124dp"
android:layout_height="60dp"
android:layout_marginStart="96dp"
android:layout_marginTop="464dp"
android:text="Add"
android:background="@color/Color"
android:onClick="buttonClickFunction"
android:textColor="@color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/CancelBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
问题是只有一个按钮似乎可以工作(cancelbutton)但是 但添加按钮似乎不起作用。
我确实声明了按钮并使用import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
TextView textView;
TextView laltitude;
TextView longitude;
Bundle bundle;
Double lal;
Double longt;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=(TextView)findViewById(R.id.textView6);
editText = (EditText) findViewById(R.id.editText1);
laltitude =(TextView)findViewById(R.id.textView2);
longitude =(TextView)findViewById(R.id.textView3);
bundle = getIntent().getExtras();
lal = bundle.getDouble("laltitude");
longt = bundle.getDouble("longitude");
laltitude.setText("laltitude : " + lal.toString());
longitude.setText("longitude : " + longt.toString());
//hhhhhhhhhhhhhhhhh going back to main activity
// CancelButton.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
// }
// });
//hhhhhhhhhhhhhhh
// AddButton.setOnClickListener({});
// if (editText.getText() == null)
// textView.setVisibility(View.VISIBLE);
// else
// textView.setVisibility(View.INVISIBLE);
// });
}
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG);
if (editText.getText().toString() == "")
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
public void Cancelonbuttonclickfunc(View view) {
startActivity(new Intent(Main2Activity.this, MainActivity.class));
}
}
方法但到目前为止没有运气。
答案 0 :(得分:2)
在按钮单击方法中,您使用==
运算符比较字符串,该运算符测试引用相等而不是值相等。这就是为什么textView
的可见性永远不会被设置为VISIBLE。要检查值的相等性,您应该使用equals
方法,如下所示:
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG).show();
if (editText.getText().toString().equals(""))
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
请注意,您也忘了将.show()
放在Toast的末尾。
答案 1 :(得分:0)
试试这个
mBtnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String text= edittext.getText().toString().trim();
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
if (text.length() <= 0) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.INVISIBLE);
}
}
});