我是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>
答案 0 :(得分:1)
您需要在按钮XML内有一个android:onClick="printHello"
才能将按钮链接到Java类内部的函数。然后,无论何时单击该按钮,它都会调用printHello并执行您所需的操作。