android 请问怎么在activity中创建多个按钮,并让这些按钮显示出来
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是按钮一"
android:onClick="onClick" />
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是按钮二"
android:onClick="onClick" />
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是按钮三"
android:onClick="onClick" />
<Button
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是按钮四"
android:onClick="onClick" />
<Button
android:id="@+id/btn_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是按钮五"
android:onClick="onClick" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_1:
Toast.makeText(MainActivity.this, ((Button)v).getText(), Toast.LENGTH_SHORT).show();;
break;
case R.id.btn_2:
Toast.makeText(MainActivity.this, ((Button)v).getText(), Toast.LENGTH_SHORT).show();;
break;
case R.id.btn_3:
Toast.makeText(MainActivity.this, ((Button)v).getText(), Toast.LENGTH_SHORT).show();;
break;
case R.id.btn_4:
Toast.makeText(MainActivity.this, ((Button)v).getText(), Toast.LENGTH_SHORT).show();;
break;
case R.id.btn_5:
Toast.makeText(MainActivity.this, ((Button)v).getText(), Toast.LENGTH_SHORT).show();;
break;
default:
break;
}
}
}