标签:wrap schema 效果 xmlns nts override new height div
直接上代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button button1,button2,button3,button4,button5,button6; TextView text1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1); button2=(Button)findViewById(R.id.button2); button3=(Button)findViewById(R.id.button3); button4=(Button)findViewById(R.id.button4); //按钮5和按钮6的点击事件是在布局文件中添加的 button5=(Button)findViewById(R.id.button5); button6=(Button)findViewById(R.id.button6); text1=(TextView)findViewById(R.id.text1); // 第一个按钮 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ text1.setText("按钮1"); } }); //第二个按钮 button2.setOnClickListener(list); //第三个按钮创建类继承点击事件 button3.setOnClickListener(new mm()); //第四个按钮本身继承点击事件 button4.setOnClickListener(this); } Button.OnClickListener list=new Button.OnClickListener(){ @Override public void onClick(View v) { text1.setText("按钮2"); } }; class mm implements View.OnClickListener{ @Override public void onClick(View v) { text1.setText("按钮3"); } } @Override public void onClick(View v) { text1.setText("按钮4"); } public void click(View v){ int id=v.getId(); switch (id){ case R.id.button5: text1.setText("按钮5"); break; case R.id.button6: text1.setText("按钮6"); break; } } }
布局文件代码:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="按钮1"
android:layout_gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="按钮2"
android:layout_gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="按钮3"
android:layout_gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:text="按钮4"
android:layout_gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:text="按钮5"
android:onClick="click"
android:layout_gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button6"
android:text="按钮6"
android:onClick="click"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text1"
android:textColor="#FF0000"
android:textSize="30dp"
android:gravity="center"
android:text="目前没有点击"
/>
</LinearLayout>
效果图:

标签:wrap schema 效果 xmlns nts override new height div
原文地址:http://www.cnblogs.com/PeachLuffy/p/6080388.html