android-常见控件的使用方法
2015-04-06 23:17
351 查看
1.常见控件的使用方法
常见的控件有:TextView,Button,EditText,ImageView,ProgressBar,AlertDialog,ProgressDialog,以下位这些控件的常见使用方法,代码中有重要注释:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:textColor="#00ff00" android:gravity="center" android:text="@string/text_view" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="2" android:hint="Type someing here"/> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" /> </LinearLayout>MainActivity.java
package com.king.uiwidgettest; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private EditText editText; private Button button; private ImageView imageView; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //注意一定要放在setContentView之上 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); 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: //注意图片名称最好有下划线,而且名称小写,否则报错 //imageView.setImageResource(R.drawable.ic_newimg); int progress = progressBar.getProgress(); progress = progress+10; System.out.println("==========aaaaaaaaaaa============"+progress); System.out.println("==========aaaaaaaaaaa============"+progressBar.getVisibility()); if(progress == 110){ progressBar.setVisibility(View.GONE); }else{ progressBar.setProgress(progress); } /* AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("This is adialog"); dialog.setMessage("error"); dialog.setCancelable(false);//表示不能通过back键取消 dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.show();*/ ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("This is ProgressDialog"); progressDialog.setMessage("loading......"); progressDialog.setCancelable(true); progressDialog.show(); /* if(progressBar.getVisibility() == View.GONE){ progressBar.setVisibility(View.VISIBLE); }else{ progressBar.setVisibility(View.GONE); }*/ /*String inputText = editText.getText().toString(); Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();*/ break; default: break; } } }
代码下载地址:http://download.csdn.net/detail/wangxuewei111/8569045
相关文章推荐
- 【Android】29、常见控件的使用方法——EditText
- 【Android】32、常见控件的使用方法——AlertDialog
- 【Android】28、常见控件的使用方法——Button
- 【Android】30、常见控件的使用方法——ImageView
- 文章标题:Android常见控件TextView的使用方法
- 文章标题:Android常见控件ImageView的使用方法
- 【Android】31、常见控件的使用方法——ProgressBar
- 【Android】33、常见控件的使用方法——ProgressDialog
- 文章标题:Android常见控件Button的使用方法
- 【Android】27、常见控件的使用方法——TextView
- Android控件系列之ImageView使用方法
- [Android]在游戏的SurfaceView中获取输入与使用EditText,Spinner等控件的方法
- Android学习整理-常见控件调用方法
- Activity_01常见控件的使用方法以及不同Activity之间的通信
- Android Gallery控件使用方法详解
- ASP.NET2.0登陆控件的使用(常见的三种方法)
- Android控件系列之RadioButton与RadioGroup使用方法
- Android常用控件-DatePicker以及对话框的两种使用方法
- Android编程 - WebView控件基本使用方法
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用