如何在不按下按钮的情况下显示此内容?

时间:2017-07-30 08:02:20

标签: javascript android android-studio jsoup

我想在不按下按钮的情况下直接显示此内容。有人能帮我吗?我是编程世界的新手。

在下面有完整的内容,如果你想更好地分析。

http://www.androidbegin.com/tutorial/android-basic-jsoup-tutorial/



 
public class MainActivity extends Activity {
 
	// URL Address
	String url = "http://www.androidbegin.com";
	ProgressDialog mProgressDialog;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
		// Locate the Buttons in activity_main.xml
		Button titlebutton = (Button) findViewById(R.id.titlebutton);
		     
		// Capture button click
		titlebutton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// Execute Title AsyncTask
				new Title().execute();
			}
		});
 
	}
 
	// Title AsyncTask
	private class Title extends AsyncTask<Void, Void, Void> {
		String title;
 
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
		}
 
		@Override
		protected Void doInBackground(Void... params) {
			try {
				// Connect to the web site
				Document document = Jsoup.connect(url).get();
				// Get the html document title
				title = document.title();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
 
		@Override
		protected void onPostExecute(Void result) {
			// Set title into TextView
			TextView txttitle = (TextView) findViewById(R.id.titletxt);
			txttitle.setText(title);
			mProgressDialog.dismiss();
		}
	}

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

将以下代码直接放在setContentView...

行下
new Title().execute();