自学Android的第一个小程序(小布局、button点击事件、toast弹出)

mac2022-06-30  32

因为上班,学习时间有限,昨晚才根据教程写了一个小程序,今天忙里偷闲写一下如何实现的,来加深一下印象。

首先创建一个Android项目,

通过activity_xxx.xml布局文件来添加组件来达到自己想要的样子

我想要的样子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" android:text="@string/question_text" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button" /> </LinearLayout> </LinearLayout> android:text="@string/question_text"中@string/question_text是引用的res/values目录下strings.xml文件中<string id="question_text">Constantinople is the largest city in Turkey.</string>的值。   但是这样布局并不能达到这样的效果: 实际运行之后是这样的:   之后问了一下是因为LinearLayout是线性布局控件,没有RelativeLayout灵活,想了解他们的区别, 请点这   之后改了一下 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="196dp" android:textSize="7pt" android:text="@string/question_text" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="76dp" android:orientation="horizontal" > <Button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /> <Button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button" /> </LinearLayout> </RelativeLayout>

这里是一些属性的设置:

android:orientation="vertical" 表示竖直方式对齐  android:orientation="horizontal"表示水平方式对齐  android:layout_width="match_parent"定义当前视图在屏幕上      可以消费的宽度,match_parent即填充整个屏幕。  android:layout_height="wrap_content":随着文字栏位的不同      而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思。  也就是视图将根据其内容自动调整大小。(表示空间的宽和高是自适应的,也就是说 他能根据text的多少来自适应控件的大小)layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。      所有的视图都有一个layout_weight值,默认为零,意思是需要显示      多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视      图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight      值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布      局的layout_weight值中所占的比率而定。      举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。      该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。      如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分      在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个       文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,      则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要      度越高)。

另外我给需要用到的组件加上了id , android:id="@+id/false_button"   加上id后,会自动在R.java下的id中自动生成你所加的id

 

要注意,@后带+是创建,例如:@+id/true_button是在R.java中创建一个名为true_button的id@后不带+是引用,例如:@string/true_button是引用res/string.xml下定义的true_button R.java位于:     之后就是要对button进行操纵了,打开xxxActivity.java文件,其位于   首先定义两个成员变量来获取button private Button mTrueButton; private Button mFalseButton;

  注:实例化变量名前加m,该前缀是Android编程所遵循的命名约定

这时软件会报错,因为我们还没有导入Button的类包,这个很简单,有自动导入类包的组合键:Ctrl+Shift+O,当然,也可以手动添加:import android.widget.Button;

现在我们就引用button组件进行操作(引用并设置监听器),点击button是弹出Toast提示信息

mTrueButton=(Button)findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show(); } }); mFalseButton=(Button)findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show(); } });

这里我们运用了Java的匿名内部类来实现。这样做的好处有二。其一,在大量代码块中,监听器方法的实现一目了然;其二,匿名内部类的使用只出现在一个地方,因此可以减少一些命名类的使用。

Toast.makeText(Context, int, int)方法:

第一个参数通常是Activity的一个实例(Activity本身就是Context的子类)。第二个参数是toast待显示字符串消息的资源ID。Toast类必须利用context才能找到并使用字符串的资源ID。第三个参数通常是两个Toast常量中的一个,用来指定toast消息显示的持续时间。

 

这样,一个button点击弹出提示消息的事例就完成了。第一次写这样的文章,相当啰嗦,相当乱,抱歉!

 

LinearLayout的内容最中间显示没弄出来,如果哪位知道请告诉一下,谢谢!

转载于:https://www.cnblogs.com/Tinoloving/p/4708367.html

最新回复(0)