常用控件使用方法

常用控件使用方法

TextView

//布局 activity_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="match_parent"
    android:layout_height="match_parent"
 >
    /*文本
        match_parent:当前控件大小与父布局大小一样,也就是父布局来决定当前控件的大小
        wrap_content:当前控件的大小刚好能包含住里面的内容,也就是由控件内容来决定当前控件的大小
        android:gravity:对齐方式
        android:textColor:颜色

    */
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#00ff00"
        android:textSize="24sp"
        android:text="This is TextView" />
</LinearLayout>

Button

//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
</LinearLayout>
//添加事件方式一
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
    	button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在此编写逻辑
            }
        });
    }
}
//添加方式二
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText;
    private ImageView imageView;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.edit_text);
        imageView = (ImageView) findViewById(R.id.image_view);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);

        button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                break;
            default:
                break;
        }
    }
}

EditText

//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
 /*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
/*输入框
 android:hint="Type somrthing here"  提示
 android:maxLines="2"   显示的行数
*/
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type somrthing here"
        android:maxLines="2"
        />
</LinearLayout>
//活动  (获取文本框的内容)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.edit_text);
        button.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                //getText()  获取文本框内容   toString()  转为字符串
                String inputText = editText.getText().toString();
                //用Toast弹出提示
                Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}

ImageView

//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
   /*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
//图片
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"
        />
</LinearLayout>
//活动 (将图片1切换为图片2)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        imageView = (ImageView) findViewById(R.id.image_view);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                //用setImageResource()将图片1改成图片2
                imageView.setImageResource(R.drawable.img_2);
                break;
            default:
                break;
        }
    }
}

ProgressBar

//布局
//圆形进度条
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    /*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
/*进度条
 android:visibility="visible"   控件可见
 android:visibility="invisible" 控件不可见,变成透明状态,依然占据原来的位置
 android:visibility="gone"      控件不可见,不再占用任何屏幕空间
*/
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
//活动  (将圆环进度条隐藏或者显示)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);

        button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                //getVisibility()  判断当前进度条是否可见
                if (progressBar.getVisibility() == View.GONE){
                    //设置当前进度条是否可见
                    progressBar.setVisibility(View.VISIBLE);
                }else{
                    progressBar.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }
    }
}


//水平进度条
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
/*进度条
 android:visibility="visible"   控件可见
 android:visibility="invisible" 控件不可见,变成透明状态,依然占据原来的位置
 andr*/
 <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"/>
</LinearLayout>
//活动  (改变进度条的进度)

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                //获取当前进度条的进度
                int progress = progressBar.getProgress();
                //在当前进度上加10
                progress = progress+10;
                //设置进度条的新进度
                progressBar.setProgress(progress);
                break;
            default:
                break;
        }
    }
}

AlertDialog

//布局  对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
</LinearLayout>
//活动 (弹出对话框)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                //通过 AlertDialog.Builder创建一个AlertDialog实例
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                //设置对话框标题
                dialog.setTitle("This is Dialog");
                //设置对话框内容
                dialog.setMessage("Something important.");
                //设置对话框可否取消
                dialog.setCancelable(false);
                //调用setPositiveButton()方法为对话框设置确定按钮的点击事件
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
					//确定事件
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
					//取消事件
                    }
                });
                //最后调用show()让对话框显示
                dialog.show();
                break;
            default:
                break;
        }
    }
}

ProgressDialog

//布局  带有进度条的对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
/*按钮
   android:textAllCaps="false"禁止字母进行大写转换
*/
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
</LinearLayout>
//活动  (弹出带有进度条的对话框)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //在此添加逻辑
                //构建一个ProgressDialog对象
                ProgressDialog proressDialog = new ProgressDialog(MainActivity.this);
                //设置标题
                proressDialog.setTitle("This is ProgressDialog");
                //设置内容
                proressDialog.setMessage("Loading.......");
                //设置可否取消
                proressDialog.setCancelable(true);
                //最后调用show()显示ProgressDialog
                proressDialog.show()
            default:
                break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值