我开发了一个包含2个文本框和1个按钮的布局。首先,按钮出现,两个文本框消失。但是,我不知道如何在按下插入按钮时更改文本框和其他组件的可见性。请解释一下因为我是android的新手。
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main3Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/textView"
android:visibility="gone" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextName"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="@+id/textView2"
android:visibility="gone" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextAddress"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:onClick="insert"
android:id="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewResult" />
</LinearLayout>
Main2Activity
package com.example.alan.mainactivity;
import org.apache.http.util.EntityUtils;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.view.View;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class Main2Activity extends ActionBarActivity {
public static final String USER_NAME1 = "USERNAME";
String myJSON;
private EditText editTextName;
private EditText editTextAdd;
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_ADD ="address";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
Button btn_insert=(Button)findViewById(R.id.button);
btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView= (TextView)findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
//Similarly the other views.....
}
});
public void insert(View view){
String name = editTextName.getText().toString();
String add = editTextAdd.getText().toString();
insertToDatabase(name, add);
}
private void insertToDatabase(String name, String add){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];
String paramAddress = params[1];
String name = editTextName.getText().toString();
String add = editTextAdd.getText().toString();
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.USER_NAME).toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("address", add));
nameValuePairs.add(new BasicNameValuePair("user", username));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://www.oobac.com/app/insert-db.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
textViewResult.setText("Inserted");
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, add);
}
//-----------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.USER_NAME);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAdd = (EditText) findViewById(R.id.editTextAddress);
TextView textView = (TextView) findViewById(R.id.textView3);
textView.setText("Welcome " + username);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
Main2Activity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
//--------------------------------------------
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.USER_NAME);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.oobac.com/app/amir.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
String st = EntityUtils.toString(response.getEntity());
Log.d("TK SUPPORT", "In the try Loop" + st);
myJSON = st;
} catch (Exception e) {
Log.d("TK SUPPORT", "Connection Error : " + e.toString());
}
return "OK";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
SAMPLE SNIPPET
replace your onCreate method to this method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.USER_NAME);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAdd = (EditText) findViewById(R.id.editTextAddress);
TextView textView = (TextView) findViewById(R.id.textView3);
Button btn_insert=(Button)findViewById(R.id.button);
btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView= (TextView)findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
//Similarly the other views.....
}
});
textView.setText("Welcome " + username);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
答案 1 :(得分:0)
试试这个:
EditText name = (EditText)findViewById(R.id.editTextName);
name.setVisibility(View.GONE);
答案 2 :(得分:0)
声明像这样的类变量
private TextView yourTextView;
您应该像这样获取TextView的实例
yourTextView = (TextView)findViewById(R.id.your_editText);
然后点击按钮,将setVisibility设置为可见
Button mButton = (Button)findViewById(R.id.m_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourTextView.setVisibility(View.VISIBLE);
}
});
隐藏,setVisibility为Gone
yourTextView.setVisibility(View.GONE);
答案 3 :(得分:0)
使用setVisibility属性隐藏您的视图。
yourView.setVisibility(View.GONE);
View.GONE此视图不可见,并且不需要任何空间用于布局。
另外
yourView.setVisibility(View.INVISIBLE);
View.INVISIBLE此视图不可见,但仍会占用空间以进行布局。
答案 4 :(得分:0)
Button btn_insert=(Button)findViewById(R.id.button);
btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView= (TextView)findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
//Similarly the other views.....
}
});
答案 5 :(得分:0)
首先,您在布局xml中设置了android:onClick="insert"
,并且您也在onCreate
方法中设置了点击监听器。因此要么从xml或onCreate侦听器中删除。 其次,在onClick
中,TextView textView =
(TextView)findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
希望这有帮助。
答案 6 :(得分:0)
以简单的方式将onClicklistener()的按钮放在onCreate()方法中,就像这样..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.USER_NAME);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAdd = (EditText) findViewById(R.id.editTextAddress);
TextView textView = (TextView) findViewById(R.id.textView3);
Button btn_insert=(Button)findViewById(R.id.button);
btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView= (TextView)findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
//do it here whatever u want to do on press of button
//Similarly the other views.....
}
});
textView.setText("Welcome " + username);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}