在Android中使用getShortClassName()方法

时间:2017-05-30 20:46:36

标签: java android class android-activity

我正在尝试修改应用程序以使用getShortClassName()方法而不是显示完整的类名称(例如“com.google.app.MainActivity”只是“.MainActivity”)。当前设置没有错误,但没有显示文本。

我认为解决方案并不复杂,尽管我没有经验可以让它发挥作用。帮助

	@Override
	public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		MyActivityInfo activity = (MyActivityInfo)getChild(groupPosition, childPosition);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.all_activities_child_item, null);

		TextView text1 = (TextView) view.findViewById(android.R.id.text1);
		text1.setText(activity.getName());

        //TextView text2 = (TextView) view.findViewById(android.R.id.text2);
        //text2.setText(activity.getComponentName().getClassName());

		TextView text2 = (TextView) view.findViewById(android.R.id.text2);
		text2.setText(activity.getShortClassName());
	
		ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
		icon.setImageDrawable(activity.getIcon());

		return view;
	}

public class MyActivityInfo implements Comparable<MyActivityInfo> {
	public MyActivityInfo(ComponentName activity, PackageManager pm) {
		this.component_name = activity;

		ActivityInfo act;
		try {
			act = pm.getActivityInfo(activity, 0);
			this.name = act.loadLabel(pm).toString();
			try {
				this.icon = (BitmapDrawable)act.loadIcon(pm);
			}
			catch(ClassCastException e) {
				this.icon = (BitmapDrawable)pm.getDefaultActivityIcon();
			}
			this.icon_resource = act.getIconResource();
		} catch (NameNotFoundException e) {
			this.name = activity.getShortClassName();
			this.icon = (BitmapDrawable)pm.getDefaultActivityIcon();
			this.icon_resource = 0;
		}

		this.icon_resource_name = null;
		if(this.icon_resource != 0) {
			try {
				this.icon_resource_name = pm.getResourcesForActivity(activity).getResourceName(this.icon_resource);
			} catch (Exception e) {}
		}
	}

	public ComponentName getComponentName() {
		return component_name;
	}

	public BitmapDrawable getIcon() {
		return icon;
	}

	public String getName() {
		return name;
	}

	public String getIconResourceName() {
		return icon_resource_name;
	}

    public String getShortClassName() {
        return class_name;
    }

	protected ComponentName component_name;
	protected BitmapDrawable icon;
	protected int icon_resource;
	protected String icon_resource_name;
	protected String name;
    protected String class_name;


	@Override
	public int compareTo(MyActivityInfo another) {
		int cmp_name = this.name.compareTo(another.name);
		if (cmp_name != 0) return cmp_name;

		int cmp_component = this.component_name.compareTo(another.component_name);
		return cmp_component;
	}

	@Override
	public boolean equals(Object other) {
		if(!other.getClass().equals(MyPackageInfo.class)) {
			return false;
		}

		MyActivityInfo other_info = (MyActivityInfo)other;
		return this.component_name.equals(other_info.component_name);
	}
}

1 个答案:

答案 0 :(得分:0)

你可以使用类对象的getSimpleName()方法:

    String a = "Hello";
    System.out.println(a.getClass().getSimpleName());
    System.out.println(a.getClass().getName());

输出:

String
java.lang.String