Android在Layout中动态增加TextView,EditText控件(自定义长度)

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private LinearLayout layout;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        changeColor();
    }
    private void init(){
        layout=findViewById(R.id.layout);
        tv=findViewById(R.id.tv);
    }
    private void changeColor(){
        tv.setText("一二三,");

        TextView tv2=new TextView(this.getApplicationContext());
        tv2.setText(",七八九");

        EditText et=new EditText(this.getApplicationContext());
        et.setWidth(42*3);

        layout.addView(et);
        layout.addView(tv2);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的基于Android Studio的登录界面示例: 1. 打开Android Studio,创建一个新的项目。 2. 在activity_main.xml文件添加以下代码: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:textSize="24sp" android:textStyle="bold" /> <EditText android:id="@+id/editTextUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:hint="Username" android:layout_marginTop="16dp"/> <EditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="Password" android:layout_marginTop="16dp"/> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="16dp"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Remember me" android:layout_marginTop="16dp"/> <Button android:id="@+id/buttonLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" android:layout_marginTop="16dp"/> </LinearLayout> ``` 3. 在MainActivity.java文件添加以下代码: ``` public class MainActivity extends AppCompatActivity { private EditText editTextUsername, editTextPassword; private RadioGroup radioGroup; private CheckBox checkBox; private Button buttonLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextUsername = findViewById(R.id.editTextUsername); editTextPassword = findViewById(R.id.editTextPassword); radioGroup = findViewById(R.id.radioGroup); checkBox = findViewById(R.id.checkBox); buttonLogin = findViewById(R.id.buttonLogin); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = editTextUsername.getText().toString(); String password = editTextPassword.getText().toString(); int selectedId = radioGroup.getCheckedRadioButtonId(); RadioButton radioButton = findViewById(selectedId); String gender = radioButton.getText().toString(); boolean remember = checkBox.isChecked(); // TODO: Add login logic here Toast.makeText(MainActivity.this, "Username: " + username + "\nPassword: " + password + "\nGender: " + gender + "\nRemember me: " + remember, Toast.LENGTH_SHORT).show(); } }); } } ``` 4. 运行应用程序,您应该看到一个登录界面,包含用户名、密码、性别、记住我和登录按钮。 这只是一个简单的示例,您可以根据自己的需求添加更多的控件和逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值