【AndroidStudio】关于AndroidStudio的常见控件TextView和Button

作者:CSDN-PleaSure乐事

欢迎大家阅读我的博客 希望大家喜欢

使用环境:AndroidStudio

1.常见控件TextView

1.1基本信息

TextView主要用于在界面上显示一段文本信息。最基本的代码格式如下:

<TextView      
    android:id="@+id/text_view"      
    android:layout_width="match_parent"     
    android:layout_height="wrap_content"      
    android:text="This is TextView" />

我们可以使用此代码来设置修改文字对齐方式,可以指定文字的对齐方式,可选值有top、bottom、left、right、center等。

android:gravity="center"

另外,我们使用textSize和textColor可以设置字体大小和颜色。

android:textSize="24sp"
android:textColor="#00ff00"

1.2应用实例

下面是一个使用textView完成的例子,供大家参考:

<TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ndroid:gravity="center"        
        android:textSize="20sp"
        android:textColor="#ff0000"
        android:text="center"/>
<TextView       
    android:id="@+id/t2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:textSize="28sp"
    android:textColor="#00ff00"
    android:text="left"/>
    
<TextView
        android:id="@+id/t3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="16sp"
        android:textColor="#0000ff"
        android:text="right"/>

最终效果如下:

2.常见控件Button

2.1基本信息

Button主要用于触发事件等,具体写法如下:

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

我们可以在.java文件当中,为button按钮注册事件监听

public class MainActivity extends Activity {
        private Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                button = (Button) findViewById(R.id.button);
                button.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        // 在此处添加逻辑
                        }
                });
        }
}

当然,我们也可以通过实现接口的方式完成事件监听,效果类似:

public class MainActivity extends AppCompatActivity {
    private ProgressBar progressbar;
    private Button button_1;
    private Button button_2;
    private EditText edittext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        progressbar = findViewById(R.id.pb_1);
        button_1 = findViewById(R.id.button_1);
        button_2 = findViewById(R.id.button_2);
        edittext = findViewById(R.id.et_1);
        button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String input = edittext.getText().toString();
                int value = Integer.parseInt(input);
                if(value>=0 && value<=100){
                    progressbar.setProgress(value);
                }
                else{
                    AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this);
                    dialog.setTitle("标题");
                    dialog.setMessage("你输入的数字不合法");
                    dialog.setCancelable(true);
                    dialog.setPositiveButton("OK",new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which){

                        }
                    });
                    dialog.show();
                }
            }
        });
    }
}

最终的效果图如下:

作者:CSDN-PleaSure乐事

希望我的博客对您有帮助,也希望在对您有帮助时您可以为我留下点赞收藏与关注,这对我真的很重要,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值