Android 开发入门-常用控件的使用方法

3.2.1 TextView




               #显示的内容

layout_width 和 wrap_content 的可选值:

  • match_parent 指定控件大小与父布局一样。
  • wrap_content 指定控件大小由控件内容决定。

gravity的可选值:

  • top 顶部对齐
  • bottom 底部对齐
  • left 左边对齐
  • right 右边对齐
  • center 等同于center_vertical|center_horizontal

3.2.2 Button

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:
                // 按钮点击处理逻辑
                break;
            default:
                break;
        }
    }

}

3.2.3 EditText

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 = (EditText) findViewById(R.id.edit_text);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                // 获取 EditText 内容
                String input = editText.getText().toString();
                Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}

3.2.4 ImageView

app/src/main/res/drawable-xhdpi/img_1.png

app/src/main/res/drawable-xhdpi/img_2.png

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:
                // 更换图片
                imageView.setImageResource(R.drawable.img_2);
                break;
            default:
                break;
        }
    }
}

3.2.5 ProcessBar

    
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);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                if (progressBar.getVisibility() == View.GONE) {
                    // 显示控件
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    // 隐藏控件
                    progressBar.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }
    }

}

水平进度条:

    
    @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;
        }
    }

3.2.6 AlertDialog

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("This is a dialog");
                dialog.setMessage("something important.");
                dialog.setCancelable(false);
                // 确定按钮的点击事件
                dialog.setPositiveButton("OK", new DiaLogInterface.OnClickListener() {
                    @Override
                    public void onClick(DiaLogInterface dialog, int whick) {
                    }
                });
                // 取消按钮的点击事件
                dialog.setNegativeButton("Cancel", new DiaLogInterface.OnClickListener() {
                    @Override
                    public void onClick(DiaLogInterface dialog, int whick) {
                    }
                });
                // 显示dialog
                dialog.show();
                break;
            default:
                break;
        }
    }
    

3.2.7 ProgressDialog

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setTitle("This is ProgressDialog");
                progressDialog.setMessage("Loading...");
                // 不能通过返回键取消,注意要在程序中处理关闭
                progressDialog.setCancelable(false);
                progressDialog.show();
                break;
            default:
                break;
        }
    }

你可能感兴趣的:(android)