1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public class MainActivity extends AppCompatActivity {
private TextView text; private Button button1;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String word = "TEXT";
button1 = (Button) this.findViewById(R.id.button); text = (TextView) this.findViewById(R.id.textView1);
button1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) {
text.setText(word); Toast.makeText(MainActivity.this, "您点击了按钮!", Toast.LENGTH_SHORT).show(); } }); } }
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_word" app:layout_constraintBottom_toTopOf="@+id/up" app:layout_constraintEnd_toEndOf="@+id/end" app:layout_constraintStart_toStartOf="@+id/start" app:layout_constraintTop_toBottomOf="@+id/buttom" tools:ignore="MissingConstraints" />
|