android 将 if else 与 getStringextra 一起使用不起作用
android using if else with getStringextra doesnt work
所以嗯,我的 if else 有问题
它只显示 else 选项而不显示 if(data == "heavy")
我只希望类型等于重而不是轻,然后其中一个线性布局消失不可见,反之亦然……请帮帮我……
这是我的代码:
Intent intentType = getIntent();
String valueType = intentType.getStringExtra("type");
EditText displayType = (EditText) findViewById(R.id.editTextType1);
displayType.setText(valueType);
LinearLayout one = (LinearLayout) findViewById(R.id.type1);
LinearLayout two = (LinearLayout) findViewById(R.id.type2);
String data = displayType.getText().toString();
if(data == "heavy") {
one.setVisibility(View.VISIBLE);
two.setVisibility(View.INVISIBLE);
} else {
one.setVisibility(View.INVISIBLE);
two.setVisibility(View.VISIBLE);
}
来自先前 .java 文件的代码:
EditText units = (EditText) findViewById(R.id.editTextUnits);
units.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(new Intent(MainActivity.this, MainActivity3.class));
Intent intentType = getIntent();
String valueType = intentType.getStringExtra("type");
intent.putExtra("type", valueType);
startActivity(intent);
}
});
另一个 .java 文件
private void SetOnClickWeightButton(){
Button btn = (Button) findViewById(R.id.buttonWeight);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("type", "heavy");
startActivity(intent);
}
});
}
private void SetOnClickLengthtButton(){
Button btn = (Button) findViewById(R.id.buttonLength);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("type", "light");
startActivity(intent);
}
});
}
使用String.equals for comparing String variables value instead of ==
所以嗯,我的 if else 有问题 它只显示 else 选项而不显示 if(data == "heavy") 我只希望类型等于重而不是轻,然后其中一个线性布局消失不可见,反之亦然……请帮帮我……
这是我的代码:
Intent intentType = getIntent();
String valueType = intentType.getStringExtra("type");
EditText displayType = (EditText) findViewById(R.id.editTextType1);
displayType.setText(valueType);
LinearLayout one = (LinearLayout) findViewById(R.id.type1);
LinearLayout two = (LinearLayout) findViewById(R.id.type2);
String data = displayType.getText().toString();
if(data == "heavy") {
one.setVisibility(View.VISIBLE);
two.setVisibility(View.INVISIBLE);
} else {
one.setVisibility(View.INVISIBLE);
two.setVisibility(View.VISIBLE);
}
来自先前 .java 文件的代码:
EditText units = (EditText) findViewById(R.id.editTextUnits);
units.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(new Intent(MainActivity.this, MainActivity3.class));
Intent intentType = getIntent();
String valueType = intentType.getStringExtra("type");
intent.putExtra("type", valueType);
startActivity(intent);
}
});
另一个 .java 文件
private void SetOnClickWeightButton(){
Button btn = (Button) findViewById(R.id.buttonWeight);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("type", "heavy");
startActivity(intent);
}
});
}
private void SetOnClickLengthtButton(){
Button btn = (Button) findViewById(R.id.buttonLength);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
intent.putExtra("type", "light");
startActivity(intent);
}
});
}
使用String.equals for comparing String variables value instead of ==