******************************************* Activity package www.bawei.com.newobj; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.support.annotation.UiThread; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Display; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements MyClick { Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); setContentView(R.layout.activity_main); init(); } }; private NewObj obj; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new Thread() { @Override public void run() { Message message = new Message(); handler.sendMessageDelayed(message, 1000); } }.start(); } void init() { View view = getWindow().getDecorView(); int fullscreen = View.SYSTEM_UI_FLAG_FULLSCREEN; view.setSystemUiVisibility(fullscreen); ActionBar actionBar = getSupportActionBar(); actionBar.hide(); obj = findViewById(R.id.tit); obj.setMyclick(this); } @Override public void myclick(View v) { Intent intent = new Intent(MainActivity.this, TwoActiity.class); switch (v.getId()) { case R.id.blak: intent.putExtra("f", "我是返回,我的id是" + v.getId()); break; case R.id.next: intent.putExtra("c", "我是next,我的id是" + v.getId()); break; } startActivity(intent); } }
//
接口
package www.bawei.com.newobj; import android.view.View; /** * Created by dell on 2017/12/29. */ interface MyClick{ void myclick(View v); }
/自定义布局package www.bawei.com.newobj; import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; /** * Created by dell on 2017/12/29. */ public class NewObj extends RelativeLayout { private MyClick myClick; public NewObj(Context context) { this(context, null); } public NewObj(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public NewObj(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); LayoutInflater inflater = LayoutInflater.from(context); // View view = from.inflate(R.layout.newlayout, this, true); View view = inflater.inflate(R.layout.newlayout, this, true); TextView blak = (TextView) view.findViewById(R.id.blak); TextView title = view.findViewById(R.id.my_title); TextView next = view.findViewById(R.id.next); blak.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { myClick.myclick(v); } }); next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myClick.myclick(v); } }); } public void setMyclick(MyClick click){ this.myClick=click; } } /// 要跳转的页面package www.bawei.com.newobj; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import android.widget.Toast; /** * Created by dell on 2017/12/29. */ public class TwoActiity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two_layout); TextView textView = (TextView) findViewById(R.id.tv1); Intent intent = getIntent(); String f = intent.getStringExtra("f"); String c = intent.getStringExtra("c"); textView.setText("我是Activity"); Toast.makeText(TwoActiity.this,f+c,Toast.LENGTH_SHORT).show(); } } // activity_layout布局<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="www.bawei.com.newobj.MainActivity"> <www.bawei.com.newobj.NewObj android:id="@+id/tit" android:layout_width="match_parent" android:layout_height="wrap_content"> </www.bawei.com.newobj.NewObj> </android.support.constraint.ConstraintLayout>///
自定义布局<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:orientation="vertical" android:background="#4f0780" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/blak" android:padding="15dp" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="@string/centext" //我在strings.xml里面定义了字符窜 android:textSize="25dp" /> <TextView android:id="@+id/my_title" android:padding="15dp" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="25dp" android:text="标题" /> <TextView android:id="@+id/next" android:padding="15dp" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="25dp" android:text="发送" /> </RelativeLayout>///
跳转的页面布局<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv1" android:text="sss" /> </android.support.constraint.ConstraintLayout>///