Last active
December 25, 2015 06:19
-
-
Save ybq/6930850 to your computer and use it in GitHub Desktop.
一个简单查找View的工具类 Demo :https://github.com/ybq/View_Finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" > | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/text1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/text2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/text3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import me.abcdef.android.view_finder.View_Finder; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.view.Menu; | |
import android.widget.TextView; | |
public class MainActivity extends Activity { | |
class Finder extends View_Finder { | |
public Finder(Context context, int layout_id) { | |
super(context, layout_id); | |
// TODO Auto-generated constructor stub | |
} | |
TextView text1; | |
TextView text2; | |
TextView text3; | |
}; | |
Finder finder = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
finder = new Finder(MainActivity.this, R.layout.activity_main); | |
setContentView(finder.parent); | |
finder.text1.setText("01"); | |
finder.text2.setText("02"); | |
finder.text3.setText("03"); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
public abstract class View_Finder { | |
/* | |
* | |
* | |
*/ | |
public View parent; | |
/** | |
* | |
* @param inflater | |
* @param context | |
* @param layout_id | |
*/ | |
public View_Finder(LayoutInflater inflater, Context context, int layout_id) { | |
parent = inflater.inflate(layout_id, null); | |
Field[] fields = getClass().getDeclaredFields(); | |
for (Field field : fields) { | |
field.setAccessible(true); | |
String field_name = field.getName(); | |
if (field_name.equals("this$0")) | |
continue; | |
if (field_name.equals("this$1")) | |
continue; | |
String package_name = context.getPackageName(); | |
try { | |
Class<?> id_class = Class.forName(package_name + ".R$id"); | |
Field res_field = id_class.getDeclaredField(field_name); | |
int value = res_field.getInt(null); | |
field.set(this, parent.findViewById(value)); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public View_Finder(Context context, int layout_id) { | |
this(LayoutInflater.from(context), context, layout_id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment