单击时按钮没有响应

代码猴子

我是新手Android Programming,我想制作一个简单的应用程序,用户可以在其中输入名称,然后单击按钮后,它会显示Hello (name of person). 按钮也会thanks for clicking me在点击后变为我已经查看了代码,但我很难找到错误。我有下面的代码。我还会在代码下添加xml。

code

public class MainActivity extends AppCompatActivity {

private EditText yourName;
private TextView outputName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    yourName = (EditText)findViewById(R.id.inputText);
    outputName = (TextView)findViewById(R.id.outputText);
}

public void printHello (View view){

    Button button =(Button) view;
    ((Button)view).setText("Thanks for Clicking Me!");

    yourName =(EditText)findViewById(R.id.inputText);
    outputName =(TextView)findViewById(R.id.outputText);

    outputName.setText("Hello, "+ yourName.getText());
    outputName.setVisibility(View.VISIBLE);

    }
}

这里也是xml

   <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="328dp"
    android:text="Talk to Me"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.078" />

   <TextView
    android:id="@+id/Label1"
    android:layout_width="0dp"
    android:layout_height="31dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="32dp"
    android:text="                      Please Enter Your Name"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

   <TextView
    android:id="@+id/outputText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="88dp"
    android:text="TextView"
    android:visibility="invisible"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/inputText"
    app:layout_constraintVertical_bias="0.0" />

   <EditText
    android:id="@+id/inputText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="72dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Label1" />

</android.support.constraint.ConstraintLayout>
基塞拉斯

您需要android:onClick="printHello"在按钮 XML 内部有一个用于将按钮链接到 Java 类内部的函数。然后,只要单击按钮,它就会调用 printHello 并执行您需要的操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章