Android学习笔记3 - Button & TextView 原创


1、修改HelloWorld程序的main.xml文件,添加如下代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:text="@string/hello"
    android:layout_height="wrap_content" 
    android:id="@+id/view1"/>
<Button 
    android:text="Button" 
    android:id="@+id/button1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/>
</LinearLayout>

2、修改HelloWordActivity.java文件,如下

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button testbut = (Button)findViewById(R.id.button1);
        final TextView testView = (TextView)findViewById(R.id.view1);
        
        testbut.setOnClickListener(new Button.OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                testView.setText("Hello TextView!");
                testView.setBackgroundColor(Color.BLUE);
            }
        });
    }
}

3、运行程序,结果如下


posted @   HFred  阅读(0)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示