Android-UI常用控件

TextView

    

button

    

匿名类方式注册监听器:

public class MainActivity extends AppCompatActivity {

    @Override
    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 view) {
            //添加逻辑
            }
        });
    }
}

ps.也可以用实现接口的方式。

EditTest

    

用Button获取文本内容

public class MainActivity extends AppCompatActivity {
    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(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputTest = editText.getText().toString();
                Toast.makeText(MainActivity.this,inputTest,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

ImageView

    
public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private ImageView imageView;
    @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);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputTest = editText.getText().toString();
                Toast.makeText(MainActivity.this,inputTest,Toast.LENGTH_SHORT).show();
                imageView.setImageResource(R.drawable.pic);
            }
        });
    }
}

ProgressBar

    
public class MainActivity extends AppCompatActivity {
    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(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputTest = editText.getText().toString();
                Toast.makeText(MainActivity.this,inputTest,Toast.LENGTH_SHORT).show();
                imageView.setImageResource(R.drawable.pic);
                if(progressBar.getVisibility()==view.GONE){
                    progressBar.setVisibility(View.VISIBLE);
                }else {
                    progressBar.setVisibility(View.GONE);
                }
                int progress=progressBar.getProgress();
                progress=progress+10;
                progressBar.setProgress(progress);
            }
        });
    }
}

AlertDialog

                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("this is a dialog");
                dialog.setMessage("balabala");
                dialog.setCancelable(false);
                dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialog.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialog.show();

ProgressDialog

                ProgressDialog progressDialog =new ProgressDialog(MainActivity.this);
                progressDialog.setTitle("this is p");
                progressDialog.setMessage("Load");
                progressDialog.setCancelable(true);
                progressDialog.show();

你可能感兴趣的:(Android)