今日所学:
AS 按钮的三种方式
(20条消息) Android studio使用小技巧之 快速生成onClick()点击方法_kill_bugs的博客-CSDN博客
遇到的问题:
一开始在虚拟机上运行一直没反应
后来在自己的手机上调试,有反应
后来新建了一个虚拟机,就可以了
还不知道问题出在哪里
明日计划:
实现App界面跳转和数据的传送
成果展示:
代码:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import static android.widget.Toast.LENGTH_LONG; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button bt3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt2).setOnClickListener(this); bt3=findViewById(R.id.bt3); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"Button 3 clickd", LENGTH_LONG).show(); } }); } public void button_nn(View view) { Toast.makeText(getApplicationContext(),"Button 1 clickd",Toast.LENGTH_LONG).show(); } public void onClick(View v) { if(v.getId()==R.id.bt2) { Toast.makeText(getApplicationContext(),"Button 2 clicked",Toast.LENGTH_LONG).show(); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="button_nn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮一" /> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮二" /> <Button android:id="@+id/bt3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮三" /> </LinearLayout>
最后祝大家 码 到成功!