在自学的路上第二次写博客,希望可以帮到和我一样的小白。话不多数,之间看代码:
1.1 MainActivity.java代码如下,代码里面有大部分有注释说明了,应该还是很清楚的,欢迎大佬指教,小小白的同志们如果有什么不清楚的地方可以给我留言哦:
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private String string = "Hello World!"; private TextView mtv; private Button mbt; private CheckBox mche; private TextView mtv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); mtv = findViewById ( R.id.mtv); mbt = findViewById ( R.id.mbt ); mche = findViewById ( R.id.mche ); mtv2 = findViewById ( R.id.mtv2 ); mtv.setOnClickListener ( new MytvOnClickLister () ); mbt.setOnClickListener ( new MybtOnClickLister () ); mche.setOnCheckedChangeListener(this); } @Override //Check的点击执行函数 public void onCheckedChanged(CompoundButton compoundButton, boolean b) { String desc = String.format("我%s",b?"被选中了":"没被选中"); compoundButton.setText(desc); } //TextView点击事件 class MytvOnClickLister implements View.OnClickListener { @Override public void onClick(View view) { if (view.getId () == R.id.mtv) { Toast.makeText ( MainActivity.this, "TextView点击", Toast.LENGTH_SHORT ).show (); mtv.setText ( "被玩坏了" ); mbt.setText ( "住手" ); mche.setChecked ( true ); mche.setText ( "我被选中了" ); //下面是一个延时程序,延时2s后执行ruu()函数 new Handler ().postDelayed ( new Runnable () { public void run() { mtv.setText (string); } }, 2000 ); } } } //Buton点击事件 class MybtOnClickLister implements View.OnClickListener { @Override public void onClick(View view) { //按钮修改Check的选中状态 /*if (view.getId () == R.id.mbt) { Toast.makeText ( MainActivity.this, "button点击", Toast.LENGTH_SHORT ).show (); mche.setChecked ( false ); mche.setText ( "我没被选中" ); }*/ //隐式Intent的用法:打开一个网页 /* Intent intent = new Intent ( Intent.ACTION_VIEW); intent.setData ( Uri.parse ( "http://www.baidu.com" ) ); startActivity ( intent );*/ //隐式Intent的用法:打开电话 /*Intent intent = new Intent ( Intent.ACTION_DIAL); intent.setData ( Uri.parse ( "tel:10086" ) ); startActivity ( intent );*/ //传递数据给SecondActivity /* String s = "我是被传递的数据"; Intent intent = new Intent ( MainActivity.this,SecondActivity.class ); intent.putExtra ( "extra_data",s ); startActivity ( intent );*/ //接收从SecondActivity返回的数据 Intent intent = new Intent ( MainActivity.this,SecondActivity.class ); startActivityForResult ( intent,1 ); } } //接收其他Activity传过来的数据第一个参数是判断是那个Activity传过来的,第二个参数是判断结果处理是否成功 protected void onActivityResult(int requestCode,int resultCode,Intent data){ switch (requestCode){ case 1: if (resultCode == RESULT_OK){ mtv2.setText ( "返回的随机数"+data.getStringExtra ( "return_data" ) ); } break; default: } } //给Activity添加菜单 public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater ().inflate ( R.menu.main,menu); return true; } //实现菜单功能 public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId ()){ case R.id.add_menu1: Toast.makeText ( this,"切换不了,放弃吧!",Toast.LENGTH_SHORT ).show (); break; case R.id.add_menu2: finish (); break; default: } return true; } }
1.2 MainActivity布局代码:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.lyp.myapplication.MainActivity"> <TextView android:id="@+id/mtv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:background="#c79898" android:textSize="30sp" android:layout_marginTop="20dp" android:layout_centerHorizontal="true" /> <Button android:id="@+id/mbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/mtv" android:text="button" android:background="@drawable/button" android:layout_marginTop="10dp" android:textAllCaps="false" android:textSize="25sp" android:layout_centerHorizontal="true" /> <CheckBox android:id="@+id/mche" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/mbt" android:layout_marginTop="10dp" android:textColor="#ff0008" android:textSize="15sp" android:button="@drawable/check" android:text="我没被选中!" android:layout_marginLeft="50dp" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/mtv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="别管我" android:background="#c79898" android:textSize="30sp" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:layout_below="@+id/mche" /> </RelativeLayout>
1.3 Button样式自定义(其实就是按下后会变个样子):
分别是:button、pressed和unpressed文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/pressed"/> <item android:drawable="@drawable/unpressed"/> </selector>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp" /> <stroke android:color="@color/colorPrimary" android:width="3dp" /> <solid android:color="@color/colorAccent"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp" /> <stroke android:color="@color/colorAccent" android:width="3dp" /> <solid android:color="@color/colorPrimary"/> </shape>
1.4 Check样式自定义:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/check_choose"/> <item android:drawable="@drawable/check_unchoose"/> </selector>
这是自定义Check是用到的选中和未选中的素材:
1.5 Mainactivity截图:
说明:其中第一个控件没什么作用,我在其中就学到了和延时函数的使用,Button的作用是跳转到其他的Activity中,接着就是一个Check复选框按钮,我在TextView和Button的点击事件中的设置了Check的选中状态,所以当点击他们的时候Check的状态会变化。不过后面奖Buton设置成跳转到SecondActivity了。最后一个也是一个TextView,他的作用主要是显示从其他的Activity传过来的数据。这里我设置的是传过来一个随机数(Math.random()函数产生0到1的随机数。返回值是double型)。所以每次传的数据都是不一样的。
2.1 SecondActivity.java代码:
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { private TextView second_mtv; private Button second_mbt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_second ); second_mtv = findViewById ( R.id.second_mtv ); second_mbt = findViewById ( R.id.second_mbt ); //接收来自MainActivity的数据 /*Intent intent = getIntent (); second_mtv.setText ( intent.getStringExtra ( "extra_data" ) ); second_mtv.setTextSize ( 30 );*/ //返回数据到MainActivity second_mbt.setOnClickListener ( new View.OnClickListener (){ @Override public void onClick(View view){ int i = (int)( Math.random()*100);//产生一个随机数 Intent intent = new Intent ( ); intent.putExtra ( "return_data",Double.toString ( i ));//将数据传输过去 setResult ( RESULT_OK,intent ); finish ();//销毁当前Activity } } ); } }
2.2 SecondActivity布局文件:
<?xml version="1.0" encoding="utf-8"?> <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:gravity="center_horizontal" tools:context="com.example.lyp.myapplication.SecondActivity"> <TextView android:id="@+id/second_mtv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="显示传递过来的数据" android:layout_marginTop="50dp" /> <Button android:id="@+id/second_mbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/second_mtv" android:layout_marginTop="20dp" android:background="@drawable/button" android:text="button" android:textAllCaps="false" android:textSize="20sp" /> </RelativeLayout>