【问题标题】:One of the Android Button work while the other does not一个 Android Button 工作,而另一个不工作
【发布时间】:2018-08-29 05:56:51
【问题描述】:

我已尽力解决问题,希望有人能帮助我找到解决方案。

我为我的活动定义了以下布局:

<?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>

相关java代码如下:

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));
}
}

问题是似乎只有一个按钮可以工作(取消按钮),但 但添加按钮似乎不起作用。

我确实声明了按钮并使用了findViewById 方法,但到目前为止还没有运气。

【问题讨论】:

    标签: android google-maps button android-activity


    【解决方案1】:

    在您的按钮单击方法中,您使用== 运算符比较字符串,该运算符测试引用相等而不是值相等。这就是为什么您的textView 的可见性永远不会设置为可见的原因。要检查值是否相等,您应该改用 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);
    }
    

    请注意,您还忘记在 Toast 末尾添加 .show()

    【讨论】:

      【解决方案2】:

      试试这个

      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);
                              }
                          }
                      });
      

      【讨论】:

        猜你喜欢
        • 2012-06-13
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 2014-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-05
        相关资源
        最近更新 更多