Android入门第一篇
2016-06-13 15:55
417 查看
本文所讲到的是LinearLayout + Button + EditText + AlertDialog的简单使用。
Activity以 LinearLayout排列,共用到两个 LinearLayout,第一个是用于全窗体,第二个用于存放两个Button,第二个 LinearLayout放在EditText控件下面,以下给出main.xml的代码:
view plaincopy
to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>
</LinearLayout>
</LinearLayout>
[xhtml] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:<a href="http://lib.csdn.net/base/15" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>
</LinearLayout>
</LinearLayout>
main.xml用于 Activity的UI设计,目前设计起来的速度,比 J2ME上的LWUIT略快(两者类似,Android提供了GUI设计工具),比WM上的.NET CF略慢(.NETCF 是RAD)。
接下来给出Java代码:
view plaincopy
to clipboardprint?
package com.studio.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
Button btnShow;
Button btnClear;
EditText edtInput;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShow=(Button)findViewById(R.id.btnShow);//控件与代码绑定
btnClear=(Button)findViewById(R.id.btnClear);//控件与代码绑定
edtInput=(EditText)findViewById(R.id.edtInput);//控件与代码绑定
btnShow.setOnClickListener(new ClickListener());//使用点击事件
btnClear.setOnClickListener(new ClickListener());//使用点击事件
}
class ClickListener implements OnClickListener
{
public void onClick(View v)
{
if(v==btnShow)
{
new AlertDialog.Builder(HelloAndroid.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Information")
.setMessage(edtInput.getText())
.show();
}
else if(v==btnClear)
{
edtInput.setText("HelloAndroid");
}
}
}
}
Activity以 LinearLayout排列,共用到两个 LinearLayout,第一个是用于全窗体,第二个用于存放两个Button,第二个 LinearLayout放在EditText控件下面,以下给出main.xml的代码:
view plaincopy
to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>
</LinearLayout>
</LinearLayout>
[xhtml] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:<a href="http://lib.csdn.net/base/15" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>
</LinearLayout>
</LinearLayout>
main.xml用于 Activity的UI设计,目前设计起来的速度,比 J2ME上的LWUIT略快(两者类似,Android提供了GUI设计工具),比WM上的.NET CF略慢(.NETCF 是RAD)。
接下来给出Java代码:
view plaincopy
to clipboardprint?
package com.studio.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
Button btnShow;
Button btnClear;
EditText edtInput;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShow=(Button)findViewById(R.id.btnShow);//控件与代码绑定
btnClear=(Button)findViewById(R.id.btnClear);//控件与代码绑定
edtInput=(EditText)findViewById(R.id.edtInput);//控件与代码绑定
btnShow.setOnClickListener(new ClickListener());//使用点击事件
btnClear.setOnClickListener(new ClickListener());//使用点击事件
}
class ClickListener implements OnClickListener
{
public void onClick(View v)
{
if(v==btnShow)
{
new AlertDialog.Builder(HelloAndroid.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Information")
.setMessage(edtInput.getText())
.show();
}
else if(v==btnClear)
{
edtInput.setText("HelloAndroid");
}
}
}
}
相关文章推荐
- Android总结篇系列:Android广播机制
- android 中xml读写
- android studio 代码混淆
- android 模拟航拍遥控
- Android百度地图应用之创建显示地图
- Android Studio 更新升级之增量升级
- 怎样在Android中使用Loaders
- android项目重构之mvp
- android项目重构之mvp
- android项目重构之mvp
- android项目重构之mvp
- android项目重构之mvp
- android项目重构之mvp
- Android中OnScrollListener的详解(Listview分批加载用到
- Android动画开发实践
- 第1个Android应用程序 Android制作简单单页导航
- LruCache缓存
- java.lang.NoSuchMethodError: android.graphics.drawable.VectorDrawable_Delegate.nDraw(JJJLandroid/gra
- 解决使用`SwipeRefreshLayout`下拉刷新和左右滑动事件冲突的问题
- android Intent 传递对象