在Android中创建具有相同大小按钮的键盘,可以通过以下步骤实现:
以下是一个示例代码:
布局文件(keyboard_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<!-- 添加更多按钮... -->
</LinearLayout>
Java代码:
public class KeyboardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyboard_layout);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 执行按钮1的操作
Toast.makeText(KeyboardActivity.this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 执行按钮2的操作
Toast.makeText(KeyboardActivity.this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
}
});
// 添加更多按钮的点击事件监听器...
}
}
这样,你就可以在Android中创建具有相同大小按钮的键盘了。根据实际需求,可以添加更多按钮,并为每个按钮设置相应的点击事件。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云